i want perform join operation on 2 distinct sets of documents having 1 field in common. have following setup.
collection a
{ "common_key" : "v1" , "a2" : "v2", "a3" : "v3", ..... }
collection b
{ "common_key" : "v1" , "b2" : "z2", "b3" : "z3", ..... }
since mapreduce can work on 1 collection merged contents of , b collection combo
. additionally, added 1 more field _datatype
indicate whether of type or b
collection combo = + b
{ "common_key" : "v1" , "b2" : "z2", "b3" : "z3", "_datatype" : "b", ..... } { "common_key" : "v1" , "a2" : "v2", "a3" : "v3", "_datatype" : "a", ..... }
i want write mapreduce function such documents have same common_key
each data type & b should output
{ "common_key" : v1 , "a" : [ { "a2" : "v2", "a3" : "v3", ... }, ... ], "b" : [ { "b2" : "z2", "b3" : "z3", ..... }, .. ] }
any pointers or appreciated
quick answer using aggregation framework
db.coll.aggregate({ "$group": { "_id": "$common_key", "a": { "$push": { "$cond": [ { "$eq": [ "$_datatype", "a" ] }, { "a2": "$a2", "a3": "$a3" // have put datatype field here }, {} ] } }, "b": { "$push": { "$cond": [ { "$eq": [ "$_datatype", "b" ] }, { "b2": "$b2", "b3": "$b3" // have put b datatype field here }, {} ] } } } })
cons: have write fields of , b datatypes project them , you'll have , empty object on each array (a , b)
Comments
Post a Comment