MongoDB avoid duplicates using $addToSet in aggregation pipeline -


there aggregation pipeline:

db.getcollection('yourcollection').aggregate(     {         $unwind: {             path: "$dates",             includearrayindex: "idx"         }     },     {         $project: {             _id: 0,             dates: 1,             numbers: { $arrayelemat: ["$numbers", "$idx"] },             goals: { $arrayelemat: ["$goals", "$idx"] },             durations: { $arrayelemat: ["$durations", "$idx"] }         }     } ) 

which perform on following data (sample documents):

{     "_id" : objectid("52d017d4b60fb046cdaf4851"),     "dates" : [         1399518702000,         1399126333000,         1399209192000,         1399027545000     ],     "dress_number" : "4",     "name" : "j. evans",     "numbers" : [         "5982",         "5983",         "5984",         "5985"     ],     "goals": [         "1",         "0",         "4",         "2"     ],    "durations": [        "78",        "45",        "90",        "90"    ] }  {     "_id" : objectid("57e250c1b60fb0213d06737c"),     "dates" : [         "1399027545000",         "1399101432000",         "1399026850000",         "1399904504000"     ],     "dress_number" : "6",     "name" : k. mitnick,     "numbers" : [         "0982",         "0981",         "0958",         "0982"     ],     "durations" : [         98,         110,         66,         92     ],     "goals" : [         "2",         "3",         "0",         "1"     ] } 

the query works good, there duplicate records i'm trying use $addtoset operator avoid duplicates:

db.getcollection('yourcollection').aggregate(         {             $match: {                 "number": number             }         },         {             $unwind: {                 path: "$dates",                 includearrayindex: "idx"             }         },          $group: {                     _id: '$_id',                     dates: { $addtoset: '$dates' }         },         {             $project: {                 _id: 0,                 dates: 1,                 numbers: { $arrayelemat: ["$numbers", "$idx"] },                 goals: { $arrayelemat: ["$goals", "$idx"] },                 durations: { $arrayelemat: ["$durations", "$idx"] }             }         }     ) 

but got dates (other field null)

{ dates:       [ '1399026850000',        '1399101432000',        '1399027545000',        '1399904504000',        '1399024474000',        '1399126333000' ],     numbers: null,     goals: null,     durations: null },   { dates:       [ '1399027545000',        '1399024474000',        '1399518702000',        '1399126333000',        '1399209192000',        '1399356651000' ],     numbers: null,     goals: null,     conversation_durations: null },   { dates:       [ '1399026850000',        '1399101432000',        '1399027545000',        '1399904504000',        '1399024474000' ],     numbers: null,     goals: null,     durations: null }  

does know problem?

you need include fields within $group pipeline using $first operator follows:

db.getcollection('yourcollection').aggregate([     { "$unwind": "$dates" },     {         "$group": {             "_id": "$_id",             "dates": { "$addtoset": "$dates" },             "numbers": { "$first": "$numbers" },             "goals": { "$first": "$goals" },             "durations": { "$first": "$durations" }         }     },     { "$unwind": {             "path": "$dates",             "includearrayindex": "idx"     } },     {         "$project": {             "_id": 0,             "dates": 1,             "numbers": { "$arrayelemat": ["$numbers", "$idx"] },             "goals": { "$arrayelemat": ["$goals", "$idx"] },             "durations": { "$arrayelemat": ["$durations", "$idx"] }         }     } ]) 

or using $setunion eliminate duplicates as:

db.getcollection('yourcollection').aggregate([     {         "$project": {             "_id": 0,             "dates": { "$setunion": ["$dates", "$dates"] },             "numbers": 1,             "goals": 1,             "durations": 1         }     }     { "$unwind": {             "path": "$dates",             "includearrayindex": "idx"     } },     {         "$project": {             "_id": 0,             "dates": 1,             "dateindex": "$idx",             "numbers": { "$arrayelemat": ["$numbers", "$idx"] },             "goals": { "$arrayelemat": ["$goals", "$idx"] },             "durations": { "$arrayelemat": ["$durations", "$idx"] }         }     } ]) 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -