简体   繁体   English

MongoDB查询带有嵌入式文档的数组(3个级别)

[英]MongoDB query with embedded document in array (3 levels)

I had this data on my MongoDB database and I would like to get only the second array about blades. 我的MongoDB数据库中有这些数据,我只想获得有关刀片的第二个阵列。

{
"_id" : ObjectId("..."),
"name" : "Westereems",
"country" : "Netherlands",
"turbines" : [
    {
        "turbine_id" : ObjectId("..."),
        "blades" : [
            {
                "blade_id" : ObjectId("..."),
                "position" : 2,
                "size" : 50,
            }
        ]
    }
]
}

I only want one return with blade_id, position and size. 我只想要一个带有blade_id,位置和大小的退货。 I tried this query and I didn't have the expectable result: 我尝试了此查询,但没有得到预期的结果:

db.collection("windfarms").find({"turbines.blades.blade_id" : ObjectId("...")}, {"turbines.blades.$" : 1, "_id" : 0})

Regards, 问候,

You can use this query, it's not an optimal query for large array's items but if you have only above items then you can use it. 您可以使用此查询,它不是用于大型数组项目的最佳查询,但是如果只有上面的项目,则可以使用它。

you can also go with aggregation to make it more optimal. 您还可以使用聚合使其更优化。

db.collection("windfarms").find({"turbines.blades.blade_id" : ObjectId("...")}, {"turbines.blades.blade_id":1,"turbines.blades.position":1,"turbines.blades.size":1,"_id":0})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM