大家好,我正在尝试使用 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));
关于java - 如何使用 java 驱动程序将文档与 mongodb 中的现有数组元素匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31980390/