2015-11-26 20 views
0

我在蒙戈這樣的數據:如何從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/[email protected]_WHITE/1.jpg", 
     "http://www.apple.com/media/pictures/tagged_items/original/[email protected]_WHITE/2.jpg", 
     "http://www.apple.com/media/pictures/tagged_items/original/[email protected]_WHITE/3.jpg", 
     "http://www.apple.com/media/pictures/tagged_items/original/[email protected]_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" : "[email protected]_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" 
} 

我怎樣才能檢索蒙戈查詢「屬性」數據每種產品?

我需要所有「uid」的「品牌」,「顏色」和「性別」。

+0

你的問題是有點不清楚,你可以展示你的預期產出與某些給定輸入什麼改進? – chridam

+1

您嘗試了什麼,您期望的輸出是什麼? – Yogesh

回答

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 
} 
+0

謝謝你的工作。如果我想檢索crated_on屬性,那麼對此有何調整?我曾嘗試添加「created_on」:1,與'uid'類似,但它沒有填充結果。你可以檢查它嗎?非常感謝。 –

+0

@VigneshPrajapati完成。 – chridam

+1

謝謝@chridam! –