大家好我想尝试使用mongodb java驱动程序匹配文档,例如:{fName : abc,lName : 456,dob : 00,address : xyz}同nameIdentity : [{fName : abc,lName : def,dob : 00,address : xyz...

大家好我想尝试使用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))));
}
我有什么错误吗?
解决方法:
UPDATE
您可能必须使用聚合框架.
也许是这样的:
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驱动程序将文档与mongodb中的现有数组元素进行匹配


基础教程推荐
猜你喜欢
- 使用JAVA实现邮件发送功能的图文教程 2023-01-09
- OpenFeign服务接口调用的过程详解 2023-06-06
- 在Java中使用LIMIT时出现MYSQL语法错误 2023-10-30
- 浅谈jsp中的9个隐含对象 2023-08-01
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- Java应用打包成Docker镜像 2023-01-24
- ThreadLocal原理介绍及应用场景 2023-08-10
- Java简明解读代码块的应用 2023-03-05
- Netty网络编程零基础入门 2023-03-22
- jsp登录页面的简单实例 雏形 2023-08-02