如何从mongo复杂子字段中提取数据?

时间:2015-11-26 15:07:16

标签: mongodb

我在mongo中有这些数据:

{
    "_id" : ObjectId("562096f4a80ea920d4281719"),
    "uid" : 162,
    "name" : "Black & White D",
    "article" : [ ],
    "attributes" : [
        {
            "name" : "brand",
            "value" : "APPLE",
            "display" : "APPLE"
        },
        {
            "name" : "color",
            "value" : "NONE",
            "display" : "Multi"
        },
        {
            "name" : "gender",
            "value" : "women",
            "display" : "Women"
        }
    ],
    "categories" : [
        ObjectId("5620961ea80ea920d42816c2"),
        ObjectId("5620961ea80ea920d42816ca")
    ],
    "collections" : [ ],
    "images" : [
        "http://www.apple.com/media/pictures/tagged_items/original/AW15N120-JK-RLJ3@BLACK_WHITE/1.jpg",
        "http://www.apple.com/media/pictures/tagged_items/original/AW15N120-JK-RLJ3@BLACK_WHITE/2.jpg",
        "http://www.apple.com/media/pictures/tagged_items/original/AW15N120-JK-RLJ3@BLACK_WHITE/3.jpg",
        "http://www.apple.com/media/pictures/tagged_items/original/AW15N120-JK-RLJ3@BLACK_WHITE/4.jpg"
    ],
    "created_on" : ISODate("2015-10-16T11:49:32.679Z"),
    "similar_products" : [
        ObjectId("562096f9a80ea920d1281694"),
        ObjectId("5641daf9a80ea94a0f11310c"),
        ObjectId("5620972ca80ea920d3281748"),
        ObjectId("562f6643a80ea96e4231ec16"),
        ObjectId("562096f8a80ea920d32816ab"),
        ObjectId("5620972ca80ea920d228170c"),
        ObjectId("56209728a80ea920d428178b"),
        ObjectId("562f5e28a80ea96e4431ec0f"),
        ObjectId("562096f7a80ea920d228169e"),
        ObjectId("562096f3a80ea920d2281697")
    ],
    "pynd_a_Sit_available" : true,
    "item_code" : "AW15N120-JK-RLJ3@BLACK_WHITE",
    "has_articles" : false,
    "is_active" : true,
    "description" : [
        {
            "text" : " perfect one.",
            "details" : [ ],
            "title" : "Style Note"
        },
        {
            "text" : "Very good product.",
            "details" : [
                {
                    "key" : "Year",
                    "value" : "2015"
                },
                {
                    "key" : "Product Sit",
                    "value" : "Regular Sit"
                },
                {
                    "key" : "Material",
                    "value" : "Synthetic"
                },
                {
                    "key" : "Season",
                    "value" : "Autumn"
                },
                {
                    "key" : "Ocassion",
                    "value" : "Casual"
                },
                {
                    "key" : "Product Type",
                    "value" : "Heavy"
                },
                {
                    "key" : "Gender",
                    "value" : "Women"
                }
            ],
            "title" : "Product Details"
        }
    ],
    "share_key" : "74bf9f"
}

如何检索"属性"每个产品的mongo查询数据?

我需要"品牌","颜色"和"性别"为所有" uid"。

1 个答案:

答案 0 :(得分:1)

使用以下聚合管道:

db.products.aggregate([
    { "$unwind": "$attributes" },
    {
        "$project": {
            "uid": 1, "created_on": 1,
            "brand": {
                "$cond": [
                    { "$eq": [ "$attributes.name", "brand" ] },
                    "$attributes.value",
                    ""
                ]
            },
            "color": {
                "$cond": [
                    { "$eq": [ "$attributes.name", "color" ] },
                    "$attributes.value",
                    ""
                ]
            },
            "gender": {
                "$cond": [
                    { "$eq": [ "$attributes.name", "gender" ] },
                    "$attributes.value",
                    ""
                ]
            }
        }
    },
    {
        "$group": {
            "_id": "$uid",
            "brand": { "$max": "$brand" },
            "color": { "$max": "$color" },
            "gender": { "$max": "$gender" },
            "created_on": { "$first": "$created_on" }
        }
    }
])

示例输出

/* 0 */
{
    "result" : [ 
        {
            "_id" : 162,
            "brand" : "APPLE",
            "color" : "NONE",
            "gender" : "women",
            "created_on" : ISODate("2015-10-16T11:49:32.679Z")
        }
    ],
    "ok" : 1
}