Java MongoDb Spring在嵌套对象中查找
我使用的是Spring Data Mongodb和以下文档:Java MongoDb Spring在嵌套对象中查找,java,regex,spring,mongodb,spring-data,Java,Regex,Spring,Mongodb,Spring Data,我使用的是Spring Data Mongodb和以下文档: { "_id" : ObjectId("565c5ed433a140520cdedd7f"), "attributes" : { "565c5ed433a140520cdedd73" : "333563851" }, "listId" : ObjectId("565c57aaf316d71fd4e4d0a0"), "international" : false, "cou
{
"_id" : ObjectId("565c5ed433a140520cdedd7f"),
"attributes" : {
"565c5ed433a140520cdedd73" : "333563851"
},
"listId" : ObjectId("565c57aaf316d71fd4e4d0a0"),
"international" : false,
"countryCode" : 33,
"deleted" : false
}
@Query("{'listId': ?0, $or: [{'attributes.*':{$regex: ?1}}], 'deleted':false}")
Page<PTContact> findByListIdAndByAttributesContaining(ObjectId listId, String val, Pageable pageable);
我想查询我的集合以搜索attributes对象中的值(在值中而不是键中)
例如:
{
"_id" : ObjectId("565c5ed433a140520cdedd7f"),
"attributes" : {
"565c5ed433a140520cdedd73" : "333563851"
},
"listId" : ObjectId("565c57aaf316d71fd4e4d0a0"),
"international" : false,
"countryCode" : 33,
"deleted" : false
}
@Query("{'listId': ?0, $or: [{'attributes.*':{$regex: ?1}}], 'deleted':false}")
Page<PTContact> findByListIdAndByAttributesContaining(ObjectId listId, String val, Pageable pageable);
@Query(“{'listId':?0$或:[{'attributes.*':{$regex:?1}}}],'deleted':false}”)
页面FindBylistandByAttributesContaining(ObjectId listId、String val、Pageable Pageable);
但我不确定能否做到这一点。有什么办法帮我吗
致意
编辑:我的解决方案
我所做的很简单。我知道该列表id的每个id(属性字段中的键),所以我创建了一个自定义mongo操作
Criteria[] fieldsCriteria = new Criteria[fields.size()];
for (int i = 0; i < fields.size(); i++) {
fieldsCriteria[i] = Criteria.where("attributes." + fields.get(i).getId()).regex(val, "i");
}
Query query = Query.query(Criteria.where("listId").is(listId).orOperator(fieldsCriteria));
return new PageImpl<>(operations.find(query.with(pageable), PTContact.class), pageable, operations.count(query, PTContact.class));
Criteria[]fieldsCriteria=新标准[fields.size()];
对于(int i=0;i
用一些页码。。
它是有效的我认为你必须改变
属性的结构
:
attributes : [{
key : "565c5ed433a140520cdedd73",
value: "333563851"
}, {
key : "...",
value: "..."
}
]
然后按如下方式更改您的请求:
{'attributes.value': ?1}
我试着这么做,但太复杂了,无法改变我项目中的一切。我的解决方案在我的第一条消息中。