below is my userpricing collection data
{
\"_id\" : ObjectId(\"584bc9ba420a6b189c510af6\"),
\"user_id\" : 1,
\"mobilenumber\":\"01234\",
\"p
You can try bulk write to bulk upload the update queries created from aggregation result and update the summary
collection.
Here is a quick code that you can try from Mongo shell and you can adjust to your needs.
The below code queries for user_id
and increments the price values based on the aggregation values and upserts if no matching user_id is found.
You should change the batch
based on your needs.
var bulk = db.getCollection('summary').initializeUnorderedBulkOp();
var count = 0;
var batch = 1;
db.getCollection('userpricing').aggregate([
{$group: {
_id:"$user_id",
Totalpositiveprice:{$sum:{$cond:[{ '$gt': ['$price', 0]}, "$price", 0]}},
Totalnegativeprice:{$sum:{$cond:[{ '$lt': ['$price', 0]}, "$price", 0]}},
Balanceprice:{"$sum":"$price"}}
},
{$project: {_id:0, user_id:"$_id", Totalpositiveprice:1, Totalnegativeprice:1, Balanceprice:1}}
]).forEach(function(doc){
var user_id = doc.user_id;
var totalpositiveprice = doc.Totalpositiveprice;
var totalnegativeprice = doc.Totalnegativeprice;
var balanceprice = doc.Balanceprice;
bulk.find({ "user_id" : user_id }).upsert().updateOne(
{ $inc: {"Totalpositiveprice" : totalpositiveprice, "Totalnegativeprice" : totalnegativeprice, "Balanceprice" : balanceprice } }
);
count++;
if (count == batch) {
bulk.execute();
bulk = db.getCollection('summary').initializeUnorderedBulkOp();
count = 0;
}
});
if (count > 0) {
bulk.execute();
}