mongodb查找子文件id等于的所有记录

时间:2014-06-24 16:35:16

标签: mongodb mongodb-query

我有以下收藏记录:

> db.products.find(ObjectId("53a9a6aad901f2961403fc9b")).pretty()
{
    "_id" : ObjectId("53a9a6aad901f2961403fc9b"),
    "code" : "N39",
    "name" : {
        "en-UK" : "N39"
    },
    "weight" : [
        90
    ],
    "collectionId" : ObjectId("53a9a6a8d901f2961403fbe2"),
    "fabric_composition" : [
        {
            "fabricId" : ObjectId("53a9a6a9d901f2961403fc69"),
            "value" : 70
        }
        {
            "fabricId" : ObjectId("53a9a6a9d901f2961403fc6a"),
            "value" : 30
        }
    ],
    "visible" : "true",
    "manufacturer" : "53a859d9d901f2e8f81ac83b"
}

> db.fabric.find().pretty()
{
    "_id" : ObjectId("53a9a6a9d901f2961403fc69"),
    "name" : [
        {
            "en-UK" : "Recycled Organic Cotton"
        }
    ]
}
{
    "_id" : ObjectId("53a9a6a9d901f2961403fc6a"),
    "name" : [
        {
            "en-UK" : "Recycled Polyester"
        }
    ]
}

如何查询mongodb集合products以列出具有ObjectId Recycled Organic Cotton的fabric_composition的所有产品作为示例?

任何建议非常感谢

1 个答案:

答案 0 :(得分:3)

您需要使用use dot notation来查询子文档:

db.products.find({
    "fabric_composition.fabricId" : ObjectId("53a9a6a9d901f2961403fc69")
});

此查询将返回至少包含一个您正在查找fabricId的子文档的所有文档。