大家好我想尝试使用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深入讲解instanceof关键字的使用 2022-11-28
- Java实现优先队列式广度优先搜索算法的示例代码 2022-09-03
- SpringBoot通过ThreadLocal实现登录拦截详解流程 2022-11-15
- SpringBoot 使用Prometheus采集自定义指标数据的方案 2023-06-06
- 如何在jsp界面中插入图片 2023-07-30
- Netty序列化深入理解与使用 2023-03-22
- java实现自定义时钟并实现走时功能 2023-01-23
- Java实现文件压缩为zip和解压zip压缩包 2023-01-24
- Java图像处理之RGB调色面板 2022-12-27
- Deep Module深模块之软件设计 2023-02-27
