Skip to content

Instantly share code, notes, and snippets.

@aonurdemir
Created June 17, 2020 08:07
Show Gist options
  • Save aonurdemir/3765ef09547fface26ef84fad7844953 to your computer and use it in GitHub Desktop.
Save aonurdemir/3765ef09547fface26ef84fad7844953 to your computer and use it in GitHub Desktop.
Join and aggregate mongo db query
db.getCollection("LOG_Android_Voided_Purchases").aggregate(
[
{
"$match" : {
"voidedTime" : {
"$gte" : ISODate("2020-06-10T00:00:00.000+0000")
}
}
},
{
"$project" : {
"productId" : 1.0
}
},
{
"$lookup": {
'from' : 'Product_Identifiers',
'localField' : 'productId',
'foreignField' : 'identifier',
'as' : 'productData'
}
},
{
"$unwind": "$productData"
},
{
"$addFields" : {
"amount" : {
"$cond" : [
{"$eq" : ["$productData.feature" , "Coins"]},
{"$toInt" : ["$productData.amount"]},
0
]
}
}
},
{
"$group" : {
"_id" : null,
"sum" : {"$sum": "$amount"}
}
}
],
{
"allowDiskUse" : false
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment