2016-10-09 6 views
1

私のmongoDBクエリには(aggregateを使って)問題があります。私はMongoCollection以下 そして、私はいくつかの手がかりを得るために喜んだろう...


KLLSであることのいずれかabまたはc、とある3 typesprocessusworkviewing。私は、クエリを達成したいのですがどのような

{_id: 1, KLLS: "a", action: "A", type: "Processus", date: Date, other:"abc" } 
{_id: 2, KLLS: "b", action: "B", type: "Processus", date: Date } 
{_id: 3, KLLS: "a", action: "C", type: "Work"  , date: Date, other:"xyz" } 
{_id: 4, KLLS: "b", action: "D", type: "Work"  , date: Date } 
{_id: 5, KLLS: "a", action: "E", type: "Viewing" , date: Date } 
{_id: 6, KLLS: "b", action: "F", type: "Viewing" , date: Date } 
... 

[ { 
    _id: { KLLS: "a"}, 
    Processus: [[action: "A", date: "Date", other: "abc"]] 
    Work:  [[action: "C", date: "Date"]] 
    Viewing: [[action: "E", date: "Date"]] 
    }, 
    { 
    _id: { KLLS: "b"}, 
    Processus: [[action: "B", date: "Date"]] 
    Work:  [[action: "D", date: "Date", other: "xyz"]] 
    Viewing: [[action: "F", date: "Date"]] 
    } 
] 

この時点で、私の(失敗)クエリは次のとおりです。

db.collection('events').aggregate([ 
    {$match: { 
     KLLS: {$in: something} 
    }}, 
    {$group: { 
     _id: { 
      klls: "$KLLS", 
      type: "$type" 
     }, 
     array: { 
      $push: "$$ROOT" 
     } 
    }} 
]) 

しかし、私は、 1つの問題がある:それはcomposite idを作るが、私はうまく3という名前を持たない。arrays ...

[ 
{ 
    _id: { KLLS: "a", type: "Processus" }, 
    array: [[action: "A", date: "Date", other: "abc"]] 
}, 
{ 
    _id: { KLLS: "a", type: "Work" }, 
    array: [[action: "C", date: "Date"]] 
}, 
{ 
    _id: { KLLS: "a", type: "Viewing" }, 
    array: [[action: "E", date: "Date"]] 
}, 
{ 
    _id: { KLLS: "b", type: "Processus" }, 
    array: [[action: "B", date: "Date"]] 
}, 
{ 
    _id: { KLLS: "b", type: "Work" }, 
    array: [[action: "D", date: "Date", other: "xyz"]] 
}, 
{ 
    _id: { KLLS: "b", type: "Viewing" }, 
    array: [[action: "F", date: "Date"]] 
}, 
... 
] 

これを解決する方法はありますか?おかげさまで


******************** EDIT ********************* *

のは非常に同じコレクションも含まれてい想像してみましょう:

{_id: 1, KLLS: "a", action: "A", type: "Processus", date: Date } 
{_id: 2, KLLS: "b", action: "B", type: "Processus", date: Date } 
{_id: 5, KLLS: "a", action: "E", type: "Viewing" , date: Date } 
{_id: 6, KLLS: "b", action: "F", type: "Viewing" , date: Date } 
    ... 
{_id: 3, KLLS: "a", action: "AB", type: "Work"  , date: Date, key:"123" } 
{_id: 4, KLLS: "b", action: "XY", type: "Work"  , date: Date, key: "123" } 
{_id: 3, KLLS: "a", action: "AB", type: "Work"  , date: Date, key:"456" } 
{_id: 4, KLLS: "b", action: "XY", type: "Work"  , date: Date, key: "456" } 
    ... 

(私は別のkeyを得た、タイプWorkのためにそれを注意してください)。あなたの助けに

おかげで、この時点で、私が得た:

[{ 
    _id: { KLLS: "a"}, 
    Processus: [ everything is ok ], 
    Viewing: [ everything is ok ], 
    Details: [ everything is ok ], 
    Work:  [ 
     {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"123" } 
     {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "123" } 
     {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"456" } 
     {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "456" } 
    ] 
... 

のに対し、私がしたい、最終的な結果は次のとおりです。

[{ 
    _id: { KLLS: "a"}, 
    Processus: [ everything is ok ], 
    Viewing: [ everything is ok ], 
    Details: [ everything is ok ], 
    Work:  [ 
     subArrayByKey [ // key = 123 
      {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"123" }, 
      {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "123" } 
     ], 
     subArrayByKey [ // key = 456 
      {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"456" }, 
      {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "456" } 
     ] 
    ] 
... 

Iは、(1をやってみました)一種のnested $groupと(2)あなたの最初の$グループに基準を追加してcomposite idにしてください。しかし$eqは...

最後に
{ $match: { something } }, 
{ $sort: { by date } }, 
{ $group: { what you wrote } }, 
{ $group: { a nested $group I tried } } 

私を当惑させている、と驚くことではないが、私は失敗した...私は..あなたが任意の手掛かりを持っていない可能性がある場合、戻りクエリには、空... ではありません...。 - )

答えて

0

の演算子を使用して、条件に一致する配列に値を追加することができます。

$condは三項演算子であり、以下の例では条件が満たされたときに元の文書が配列に追加され、条件が満たされていない場合はダミーの値falseが配列に追加されます。

最後の$projectステージでは、setDifference演算子を使用して、配列内のダミーのfalseエントリが削除されます。

db.events.aggregate([ 
    {"$group": { 
     "_id" : "$KLLS", 
     "Processus": { 
      "$push": { 
       "$cond": [{"$eq": ["$type", "Processus"]}, "$$ROOT", false] 
      } 
     }, 
     "Work": { 
      "$push": { 
       "$cond": [{"$eq": ["$type","Work"]}, "$$ROOT", false] 
      } 
     }, 
     "Viewing": { 
      "$push": { 
       "$cond": [{"$eq" : ["$type","Viewing"]}, "$$ROOT", false] 
      } 
     }, 
     "Details": {"$push" : "$other"} 
    }}, 

    {"$project": { 
     "_id": 0, 
     "KLLS": "$_id", 
     "Processus": {"$setDifference": ["$Processus", [false]]}, 
     "Work":  {"$setDifference": ["$Work",  [false]]}, 
     "Viewing": {"$setDifference": ["$Viewing", [false]]}, 
     "Details": 1 
    }} 
]) 

また、アレイにほんの少数のフィールドを追加するには、以下のようなものを試すことができます。

db.events.aggregate([ 
    {"$group": { 
     "_id": "$KLLS", 
     "Processus": { 
      "$push": { 
       "$cond": [{"$eq": ["$type","Processus"]}, {"action": "$action","other": "$other"}, false] 
      } 
      }, 
      "Work": { 
       "$push": { 
        "$cond": [{"$eq": ["$type","Work"]}, {"action": "$action","other": "$other"}, false] 
       } 
      }, 
      "Viewing":{ 
       "$push": { 
        "$cond": [{"$eq": ["$type","Viewing"]}, {"action": "$action","other": "$other"}, false] 
       } 
      }, 
      "Details": {"$push": "$other"} 
    }}, 

    {"$project": { 
     "_id": 0, 
     "KLLS": "$_id", 
     "Processus": {"$setDifference": ["$Processus", [false]]}, 
     "Work":  {"$setDifference": ["$Work",  [false]]}, 
     "Viewing": {"$setDifference": ["$Viewing", [false]]}, 
     "Details": 1 
    }} 
]) 
+0

まずは、ありがとうございます。それは魅力のように_ほとんど_うまくいきます! 'Action: 'で私のレコードから取り出した' other: "abc" 'を含む' Details'という別の配列( 'Processus'、' Work'、 'Viewing'のような)' Group'にも感謝してもいいですか? A "(私の最初の記事を見てください)。もう一度ありがとうございます:MongoDBのクエリとagregateは簡単ではありません... – nerotulip

+0

もう一度ありがとうございます。私は最初の投稿を編集して、 'ネストされたグループ'または '複合ID'に関する最終的な質問をします。それほど私を助けてくれるのはあなたの親切なことです! – nerotulip

+0

最新の編集部分を別の質問として投稿してください。より多くのユーザーを引き付けるだろう。 – 4J41