MongoDB unwind multiple arrays -


in mongodb there documents in following structure:

{     "_id" : objectid("52d017d4b60fb046cdaf4851"),     "dates" : [         1399518702000,         1399126333000,         1399209192000,         1399027545000     ],     "dress_number" : "4",     "name" : "j. evans",     "numbers" : [         "5982",         "5983",         "5984",         "5985"     ] } 

is possible unwind data multiple arrays , paired elements arrays:

{     "dates": "1399518702000",     "numbers": "5982" }, {     "dates": "1399126333000",     "numbers": "5983" }, {     "dates": "1399209192000",     "numbers": "5984" }, {     "dates": "1399027545000",     "numbers": "5985" } 

from version 3.2 can $unwind on both of arrays, $cmp indexes, , $match equal indexes.

this solution populate wrote in case have example document. if have more documents don't know expect in output, it's solvable grouping _id of document.

db.test.aggregate([     {         $unwind: {             path: '$dates',             includearrayindex: 'dates_index',         }     },     {         $unwind: {             path: '$numbers',             includearrayindex: 'numbers_index',         }     },     {         $project: {             dates: 1,             numbers: 1,             compare: {                 $cmp: ['$dates_index', '$numbers_index']             }         }     },     {         $match: {             compare: 0         }     },     {         $project: {             _id: 0,             dates: 1,             numbers: 1         }     } ]) 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -