below is my userpricing collection data
{
"_id" : ObjectId("584bc9ba420a6b189c510af6"),
"user_id" : 1,
"mobilenumber":"01234",
"price" : 2000.0,
"type" : "credit",
},
{
"_id" : ObjectId("584bc9ba420a6b189c510af6"),
"user_id" : 1,
"mobilenumber":"01234",
"price" : -1000.0,
"type" : "credit",
},
{
"_id" : ObjectId("584bc9ba420a6b189c3323w23"),
"user_id" : 2,
"mobilenumber":"04321",
"price" : 1000.0,
"type" : "credit",
}
here i want to calculate total postive and total negative price of all user and i need to check whether that user is exists or not in summary collection.if record not exists we need to create document in summary collection if its exists we need to update "Totalpositiveprice","Totalnegativeprice" and "Balanceprice"
in summary table already exists this record
{
"user_id": "1",
"mobilenumber":"01234",
"Totalpositiveprice": 3000.0,
"Totalnegativeprice": 0,
"Balanceprice": 3000.0
},
{
"user_id": "3",
"mobilenumber":"05555",
"Totalpositiveprice": 1000.0,
"Totalnegativeprice": -100,
"Balanceprice": 900.0
}
we need to update the document for "mobilenumber":"01234",
we need to create new document for "mobilenumber":"04321",
"mobilenumber":"05555" no need to do anything bcoz nothing is there in userpricing
finally i should get summary collection like this
{
"user_id": "1",
"mobilenumber":"01234"
"Totalpositiveprice": 5000.0,
"Totalnegativeprice": -1000.0,
"Balanceprice": 4000.0
},
{
"user_id": "2",
"mobilenumber":"04321"
"Totalpositiveprice": 1000.0,
"Totalnegativeprice": 0,
"Balanceprice": 1000.0
},
{
"user_id": "3",
"mobilenumber":"05555",
"Totalpositiveprice": 1000.0,
"Totalnegativeprice": -100,
"Balanceprice": 900.0
}
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.Whatever you want to do involves a complex query. See the below query. Let me explain it first.
1- first pipeline
$group
is obvious to you.2- second pipeline
$lookup
is required to generate your desired result as you want that the current prices and the already summarized prices should be compiled into one document.3- third stage is just unwinding the array
summary
we have looked up so that we will be able to compile the prices.4- fourth
$project
stage is doing what you really want to do that is to say that it is compiling the summary document you will be able to understand it.5- generating the summary collection.
As from other answers, the problem with
$out
is that it will replace thesummary
collection if one exists and you may not want that. You can use a little JavaScript for that.You can do it by using
conditional sum
and export in another collection use$out: collectionName
can try it :
N.B: result exported in summary collection