I have a document with entries like this
{
"_id": ObjectId("5644c495d0807a1750043237"),
"siteid": "123456"
"amount": 1.32
}
Some documents have other amounts eg."cashbackAmount"
I want a sum and a count for each amount fields. Not every document contains all the amount fields.
I hjave tried the following
{
$group: {
"_id": "$siteid",
item2: { "$sum": "$amount" },
item3: { "$sum": "$totalAmount" },
item4: { "$sum": "$cashbackAmount" },
item5: { "$sum": "$unitPrice" },
}
}
It gives me the sum, but I cannot work out how to get the number times each amount field is present.
{ "$sum": 1 } does not work because that gives me all the documents that have any one of the totals fields.
get the number times each amount field is present? Can you give some expected output example?