I have the following data structure:
{
"_superBill": {
"$oid": "568b250ba082dfc752b20021"
},
"paymentProviderTxID": "aaaa",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-26T13:04:05.544Z"
}
},
{
"_superBill": {
"$oid": "568b250ba082dfc752b20021"
},
"paymentProviderTxID": "bbbb",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-26T13:04:05.544Z"
}
},
{
"_superBill": null,
"paymentProviderTxID": "cccc",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-27T13:04:05.544Z"
}
},
{
"_superBill": null,
"paymentProviderTxID": "dddd",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-28T13:04:05.544Z"
}
}
I have an Aggregate function that groups by the referenced _superBill field. It works great until I run into entries that have null values for _superBill and groups them all into one.
Is there a way that I can group only the entries that have valid _superBill while still including the values that have 'null' so that I can do this in one query and sort them by visitDate?
Aggregate function:
Transaction.aggregate([
{ $match: { paymentDate: { $gte: period.periodStart, $lt: period.periodEnd }} },
{
$group: {
_id: "$_superBill",
visits: { $sum: 1 }
}
},
{
$project: {
_id: '$_id',
superBill: '$_id',
visits: '$visits',
visitDate: '$visitDate',
commissionRate: '$commissionRate'
}
}
], function(err,results) {
// Process results
});
The result set would look like this. Note that the first 2 are grouped because they have the same _superBill _id and the other two are left ungrouped.
{
"_superBill": {
"$oid": "568b250ba082dfc752b20021"
},
"visits": 1,
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-26T13:04:05.544Z"
}
},
{
"_superBill": null,
"visits": 1,
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-27T13:04:05.544Z"
}
},
{
"_superBill": null,
"visits": 1,
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-28T13:04:05.544Z"
}
}
Thanks for looking and appreciate any help.
You can use the _id
to $group
your documents if _superBill
equals null
. To do that you can use the $ifNull
operator.
Transaction.aggregate(
[
{ "$group": {
"_id": { "$ifNull": [ "$_superBill", "$_id" ] },
"visits": { "$sum": 1 },
"visitDate": { "$first": "$visitDate" },
"commissionRate": { "$first": "$commissionRate" },
"_superBill": { "$first": "$_superBill" }
}}
], function(err,results) {
// Process results
}
)
Of course you can also use the $cond
operator.
"_id": {
"$cond": [
{ "$eq": [ "$_superBill", null ] },
"$_id",
"$_superBill"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With