2017-08-23 205 views
0

我有一個集合的命令,裏面收集我有屬性陣列,我想小組attributes.value其中attributes.name是「sugery」MongoDB中查詢到陣列

db.orders.find({},{"attributes":1}).sort({_id:-1}).limit(3); 
{ 
    "_id" : ObjectId("599cfdb0f6ec8d00015b0880"), 
    "attributes" : [ 
     { 
      "id" : ObjectId("5979a220ad86210001997d5a"), 
      "name" : "patient", 
      "status" : NumberInt(1), 
      "sysDefined" : NumberInt(1), 
      "value" : "Lincon Burrows" 
     }, 
     { 
      "id" : ObjectId("5979a229ad86210001997d5c"), 
      "name" : "notes", 
      "status" : NumberInt(1), 
      "sysDefined" : NumberInt(1), 
      "value" : "" 
     }, 
     { 
      "id" : ObjectId("5979a230ad86210001997d5e"), 
      "name" : "surgeon", 
      "status" : NumberInt(1), 
      "sysDefined" : NumberInt(1), 
      "value" : "Dr. Williams" 
     }, 
     { 
      "id" : ObjectId("5979a238ad86210001997d60"), 
      "name" : "surgery", 
      "status" : NumberInt(1), 
      "sysDefined" : NumberInt(1), 
      "value" : "Heart Surgery" 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("599c357a9ec26c00017892a6"), 
    "attributes" : [ 
     { 
      "value" : "new patient", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "patient", 
      "id" : ObjectId("5979a220ad86210001997d5a") 
     }, 
     { 
      "value" : "", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "notes", 
      "id" : ObjectId("5979a229ad86210001997d5c") 
     }, 
     { 
      "value" : "http://localhost:4200/#new surgeon", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "surgeon", 
      "id" : ObjectId("5979a230ad86210001997d5e") 
     }, 
     { 
      "value" : "new surgery", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "surgery", 
      "id" : ObjectId("5979a238ad86210001997d60") 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("599c2069b2afd900019ea142"), 
    "attributes" : [ 
     { 
      "value" : "Mellisa Darwin", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "patient", 
      "id" : ObjectId("5979a220ad86210001997d5a") 
     }, 
     { 
      "value" : "", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "notes", 
      "id" : ObjectId("5979a229ad86210001997d5c") 
     }, 
     { 
      "value" : "Dr. Josefina P. Robles", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "surgeon", 
      "id" : ObjectId("5979a230ad86210001997d5e") 
     }, 
     { 
      "value" : "Spine Surgery", 
      "sysDefined" : NumberInt(1), 
      "status" : NumberInt(1), 
      "name" : "surgery", 
      "id" : ObjectId("5979a238ad86210001997d60") 
     } 
    ] 
} 
記錄

我想輸出像

[ 
    { 
     "_id":"Heart Surgery", 
     "count":1 
    }, 
    { 
     "_id":"new surgery", 
     "count":1 
    }, 
    { 
     "_id":"Spine Surgery", 
     "count":1 
    } 
] 

回答

0

請參閱組的mongodb文檔。 db.collection.group()

以下是db.collection.group()方法的示例。由ord_dtitem.sku領域那些有文件

{ 
    _id: ObjectId("5085a95c8fada716c89d0021"), 
    ord_dt: ISODate("2012-07-01T04:00:00Z"), 
    ship_dt: ISODate("2012-07-02T04:00:00Z"), 
    item: { 
     sku: "abc123", 
     price: 1.99, 
     uom: "pcs", 
     qty: 25 
    } 
} 

集團利用兩個場

下面的例子組:這些示例假設一個訂單收集用以下原型文件ord_dt大於01/01/2012

db.runCommand({ 
group: 
    { 
    ns: 'orders', 
    key: { ord_dt: 1, 'item.sku': 1 }, 
    cond: { ord_dt: { $gt: new Date('01/01/2012') } }, 
    $reduce: function (curr, result) { }, 
    initial: { } 
    } 
}) 
+0

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/評論/低質量帖/ 17117743) –

+0

@Szymon Stepniak謝謝你的意見 –

0

試試這個:

orders.aggregate({"attributes":1}, {$unwind:"$attributes"},{$match:{"attributes.name":"surgery"}})