Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- db.users.aggregate([
- { "$group" : { "__tmp0" : { "$push" : "$$ROOT"} , "_id" : "$_id"}},
- { "$project" : { "_id" : "$_id" , "value" : { "left" : "$__tmp0" , "right" : { "$literal" : [ ]} , "_id" : "$_id"}}},
- { "$out" : "tmp.gen_0"}
- ],
- { allowDiskUse: true })
- db.posts.mapReduce(
- function () {
- emit.apply(null, (function (key, value) {
- return [(value != null) ? value.user : undefined, {
- "left": [],
- "right": [value]
- }];
- })(this._id, this));
- },
- function (key, values) {
- var result = {
- "left": [],
- "right": []
- };
- values.forEach(function (value) {
- result.left = result.left.concat(value.left);
- result.right = result.right.concat(value.right);
- });
- return result;
- },
- { "out" : { "reduce" : "tmp.gen_0" , "nonAtomic" : true}})
- db.tmp.gen_0.aggregate([
- { "$match" : { "value.left" : { "$not" : { "$size" : 0}} , "value.right" : { "$not" : { "$size" : 0}}}},
- { "$unwind" : "$value.left"},
- { "$unwind" : "$value.right"},
- { "$group" : { "cnt" : { "$sum" : { "$literal" : 1}} , "displayName" : { "$push" : "$value.left.displayName"} , "_id" : "$value.right.user"}},
- { "$unwind" : "$displayName"},
- { "$group" : { "__tmp1" : { "$first" : "$$ROOT"} , "_id" : { "0" : "$cnt" , "1" : "$displayName"}}},
- { "$project" : { "cnt" : "$__tmp1.cnt" , "displayName" : "$__tmp1.displayName" , "_id" : false}},
- { "$out" : "tmp.gen_1"}
- ],
- { allowDiskUse: true })
- db.tmp.gen_0.drop()
- db.tmp.gen_1.find()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement