如何通过内部映射中的参数查找 mongo 文档(使用 Spring MongoTemplate 效果更好)
How to find mongo document by parrameter in internal map (better with Spring MongoTemplate)
我有 collection 文档:
{
a:"a1",
b:{
"bla1-1":{c:1,d:2},
"bla1-2":{c:3,d:4}
}
},
{
a:"a2",
b:{
"bla2-1":{c:1,d:2},
"bla2-2":{c:5,d:6}
}
}
如何找到包含 c == 5 的文档?就我而言:
{
a:"a2",
b:{
"bla2-1":{c:1,d:2},
"bla2-2":{c:5,d:6}
}
}
P.S。我在我的应用程序中使用 Spring MongoTemplate。并且在回答中看到MongoTemplate的用法会更好。
使用纯 mongo 无法做到这一点,我建议更改架构。
但这可以使用 $where
:
来完成
db.test.find({
$where: function () {
for (var prop in this.b) {
if (this.b[prop].c == 5) {
return true;
}
}
return false;
}
})
我有 collection 文档:
{
a:"a1",
b:{
"bla1-1":{c:1,d:2},
"bla1-2":{c:3,d:4}
}
},
{
a:"a2",
b:{
"bla2-1":{c:1,d:2},
"bla2-2":{c:5,d:6}
}
}
如何找到包含 c == 5 的文档?就我而言:
{
a:"a2",
b:{
"bla2-1":{c:1,d:2},
"bla2-2":{c:5,d:6}
}
}
P.S。我在我的应用程序中使用 Spring MongoTemplate。并且在回答中看到MongoTemplate的用法会更好。
使用纯 mongo 无法做到这一点,我建议更改架构。
但这可以使用 $where
:
db.test.find({
$where: function () {
for (var prop in this.b) {
if (this.b[prop].c == 5) {
return true;
}
}
return false;
}
})