2017-08-23 14 views
0

に私はグループに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") 
     } 
    ] 
} 
ある attributes.value上のレコードをしたい、コレクション内の私は、属性の配列を持つ午前、コレクションの受注を持っています

私は

[ 
    { 
     "_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"}}) 
関連する問題