如何使用java驱动程序将文档与mongodb中现有的数组元素进行匹配

如何使用java驱动程序将文档与mongodb中现有的数组元素进行匹配,java,mongodb,mongodb-query,Java,Mongodb,Mongodb Query,您好,我正在尝试使用mongodb java驱动程序匹配文档,例如: { "fName" : "abc", "lName" : "456", "dob" : "00", "address" : "xyz" } 与 如果我找到了文档,那么我不会做任何其他事情来添加文档。这里的问题是,如果我的源文档包含fname:abc和lname:456,那么这与第一组nameIdenti

您好,我正在尝试使用mongodb java驱动程序匹配文档,例如:

     {
             "fName" : "abc",
             "lName" : "456",
             "dob" : "00",
             "address" : "xyz"
     }

如果我找到了文档,那么我不会做任何其他事情来添加文档。这里的问题是,如果我的源文档包含fname:abc和lname:456,那么这与第一组nameIdentity中的fname和第二组identity中的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 nameIdentity=(List)matchedDocument.get(“nameIdentity”);
对于(int i=0;i更新
您可能必须使用聚合框架

可能是这样的:

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));
List filterList=new ArrayList();
add(newbsondocument().append(“nameIdentity.fName”,newbsonString(“abc”)));
add(new BsonDocument().append(“nameIdentity.lName”,new BsonString(“456”));
FindIterable it=collection.find(Filters.and(filterList));

没有,我看到了上面的链接。我想我的问题是不同的。只是想澄清一下你的意图。如果“fname”或“lname”属性在另一个数组元素中匹配,你是想避免推送一个新的数组元素?还是只寻找一个unqiue属性,如“fname”在ant数组元素中不存在?那么期望的响应是什么?报告nothing modified或其他内容?
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));