[英]MongoDB : how to get all elements that contain an array using Java Driver?
[英]How to match a document with existing array elements in mongodb using java driver
大家好我想尝试使用mongodb java驱动程序匹配文档,例如:
{
"fName" : "abc",
"lName" : "456",
"dob" : "00",
"address" : "xyz"
}
同
"nameIdentity" : [
{
"fName" : "abc",
"lName" : "def",
"dob" : "00",
"address" : "xyz"
},
{
"fName" : "123",
"lName" : "456",
"dob" : "00",
"address" : "789"
}
如果我找到了该文件,那么我不做任何其他事情添加文件。 我的问题是如果我的源文档包含fname:abc和lname:456这是第一组nameIdentity中的fname和第二组标识中的lname匹配。 我希望这是一个完整的匹配。 我尝试过这样的事情
List<Document> nameIdentities = (List<Document>) matchedDocument.get("nameIdentity");
for (int i=0;i<nameIdentities.size();i++)
{
temp.add(nameIdentities.get(0));
quBasicDBObject=new BasicDBObject("$and",temp);
}
iterable = mongoDatabase.getCollection("entity").find(updatedDocumentTypeOne);
if (iterable.first() == null)
{
updateResult = mongoDatabase.getCollection("entity")
.updateOne(
new Document("_id", new ObjectId(objectId)),
new Document("$push", new Document("nameIdentity", nameList.get(0))));
}
我有什么错误吗?
更新您可能必须使用聚合框架。
也许是这样的:
List<Bson> filterList = new ArrayList<>();
filterList.add(new BsonDocument().append("nameIdentity.fName", new BsonString("abc") ));
filterList.add(new BsonDocument().append("nameIdentity.lName", new BsonString("456") ));
FindIterable<org.bson.Document> it = collection.find(Filters.and(filterList));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.