I have performed aggregation to calculate the total and the objects that contribute to the total.
Now, I need to update the source table with the aggregated object ID for the elements involved in the aggregation, establishing a two-way relationship.
coll.aggregate([
{ "$match": {"elig": 1, "nid" : null, "cncl" : null } },
{ "$group": {
"_id": "$nkey",
"cumqty": {"$sum": "$pr_qty.qty" },
"netted" : { "$push" : "$_id" }
}},
{ "$project": {
"nkey":"$nkey" ,
"cumqty": "$cumqty",
"netted" : "$netted" ,
"_id" : 0
}},
{ "$out": aggcollnm }
])
The aggregated table now contains a list of object IDs created with $push.
For example, let's say doc1, doc2, and doc3 have contributed to the creation of agg1, and agg1 includes doc1, doc2, and doc3 in its list. I want doc1, doc2, and doc3 to have the ID of agg1 as their nettid.
So, I implemented the following solution:
coll.find().forEach( function(elem) {
coll.update (
{ "_id" : elem._id },
{ "$set" : { nid : aggcoll.aggregate ( [
{ "$unwind" : "$netted" } ,
{ "$match" : { "netted" : elem._id } },
{ "$project" : { "_id" :1 } }
] )._firstBatch[0]
}}
)
})
This solution worked well with a smaller dataset, but for 1 million documents, it fails with the following error:
2014-06-30T09:48:40.577+0100 Error: getMore: cursor didn't exist on server, possible restart or timeout? at src/mongo/shell/query.js:116 failed to load: ./netting.js
Is there a more efficient way to achieve this task?