需要查找查询 mongodb 中的动态多个嵌套集合



我需要根据值嵌套查找集合。但是我的收藏具有动态值.请参阅下面的代码。其中图像名称键是动态的(_DSC9691.jpg,_DSC9514.JPG),而"key1"是动态项。现在我需要根据组件、材料、子类型查找集合

{ 
"_id" : ObjectId("5ce2df8498f10b276cb466c4"), 
"num" : "1", 
"lat" : "39.941436099965", 
"lon" : "-86.0691700063581", 
"images" : {
"_DSC9691.jpg" : {
"key1" : {
"component" : "Sleeve", 
"condition" : "", 
"sub_type" : {
"Auto Sleeve" : true
}, 
"material" : "", 
"misc" : ""
}
}
}}
{ 
"_id" : ObjectId("5ce2df8498f10b276cb466c7"), 
"num" : "4", 
"lat" : "39.9413828961847", 
"lon" : "-86.0715084495015", 
"images" : {
"_DSC9554.JPG" : {
}, 
"_DSC9514.JPG" : {
}, 
"_DSC9622.JPG" : {
} 

}}

@Nagendran您将无法执行这些操作,因为您要监视的嵌套文档未正确命名。我建议您使用公用名重命名该字段,并尝试使用下面的代码。还要记住不要在字段名称上使用空格,例如"自动套筒"。

对象:

{ 
"_id" : ObjectId("5ce2df8498f10b276cb466c7"), 
"num" : "4", 
"lat" : "39.9413828961847", 
"lon" : "-86.0715084495015", 
"images" : [
{
"name" : "some name",
"key":
{
"component" : "Sleeve", 
"condition" : "", 
"sub_type" : {
"Auto_Sleeve" : true
}, 
"material" : "", 
"misc" : ""
}, 
},
{
"name" : "some name 2",
"key":
{
"component" : "Sleeve 2", 
"condition" : "", 
"sub_type" : {
"Auto_Sleeve" : true
}, 
"material" : "", 
"misc" : ""
}, 
},
]
}

查询:

db.collection.find({
"images.key.sub_type.Auto_Sleeve": true
})

如果需要,可以使用聚合框架在"图像"嵌套文档中进行过滤。

要获得更多信息,您可以访问:

https://docs.mongodb.com/manual/aggregation/

https://docs.mongodb.com/manual/tutorial/query-documents/

https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1

https://university.mongodb.com/

最新更新