2016-11-25 1 views
0

(Mongo Neuling hier, sorry) Ich habe eine mongodb Sammlung, Ergebnis einer mapreduce mit diesem Schema:sortiert eine Übereinstimmung Gruppe von ID in Aggregate

{ 
    "_id" : "John Snow", 
    "value" : { 
    "countTot" : 500, 
    "countCall" : 30, 
    "comment" : [ 
     { 
     "text" : "this is a text", 
     "date" : 2016-11-17 00:00:00.000Z, 
     "type" : "call" 
     }, 
     { 
     "text" : "this is a text", 
     "date" : 2016-11-12 00:00:00.000Z, 
     "type" : "visit" 
     }, 
     ... 
    ] 
    } 
} 

Mein Ziel ist es, ein Dokument zu haben, alle Bemerkungen, eines bestimmten Typs. Zum Beispiel, ein Dokument John Schnee mit allen Anrufen.

wo ich all die Kommentare für eine bestimmte Art zu haben, mit diesen:

db.general_stats.aggregate(

    { $unwind: '$value.comment' }, 

    { $match: { 
     'value.comment.type': 'call' 
    }} 
) 

Aber ich kann nicht einen Weg zu gruppieren die Daten durch die ID (zum Beispiel John Snow) auch erhalten finden mit die $ group-Eigenschaft Irgendeine Idee ?

Danke fürs Lesen.

Antwort

1

Hier ist die Lösung für Ihre Anfrage.

db.getCollection('calls').aggregate([ 
    { $unwind: '$value.comment' }, 
    { $match: { 
     'value.comment.type': 'call' 
    }}, 
    { 
     $group : { 
      _id : "$_id", 
      comment : { $push : "$value.comment"}, 
      countTot : {$first : "$value.countTot"}, 
      countCall : {$first : "$value.countCall"}, 
     } 
    }, 
    { 
     $project : { 
      _id : 1, 
      value : {"countTot":"$countTot","countCall":"$countCall","comment":"$comment"} 
     } 
    } 
]) 

oder können Sie entweder mit $ Projekt mit $ filter Option

db.getCollection('calls').aggregate([ 
    { 
     $project: { 
     "value.comment": { 
      $filter: { 
       input: "$value.comment", 
       as: "comment", 
       cond: { $eq: [ "$$comment.type", 'call' ] } 
      } 
     }, 
     "value.countTot":"$value.countTot", 
     "value.countCall":"$value.countCall", 
     } 
    } 
]) 

In beiden Fällen gehen unter meinem ausgegeben wird.

{ 
    "_id" : "John Snow", 
    "value" : { 
     "countTot" : 500, 
     "countCall" : 30, 
     "comment" : [ 
      { 
       "text" : "this is a text", 
       "date" : "2016-11-17 00:00:00.000Z", 
       "type" : "call" 
      }, 
      { 
       "text" : "this is a text 2", 
       "date" : "2016-11-17 00:00:00.000Z", 
       "type" : "call" 
      } 
     ] 
    } 
} 
0

Hier ist die Abfrage, die die Erweiterung der in OP vorhandenen ist.

db.general_stats.aggregate(
    { $unwind: '$value.comment' }, 
    { $match: { 
     'value.comment.type': 'call' 
    }}, 
    {$group : {_id : "$_id", allValues : {"$push" : "$$ROOT"}}}, 
    {$project : {"allValues" : 1, _id : 0} }, 
    {$unwind : "$allValues" } 
); 

Ausgang: -

{ 
    "allValues" : { 
     "_id" : "John Snow", 
     "value" : { 
      "countTot" : 500, 
      "countCall" : 30, 
      "comment" : { 
       "text" : "this is a text", 
       "date" : ISODate("2016-11-25T10:46:49.258Z"), 
       "type" : "call" 
      } 
     } 
    } 
} 
Verwandte Themen