Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb Aggregate $group '_id' null values as distinct

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.

like image 791
daxiang28 Avatar asked Jan 06 '16 14:01

daxiang28


1 Answers

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" 
    ] 
}
like image 138
styvane Avatar answered Nov 05 '22 17:11

styvane