deepu
deepu

Reputation: 2029

Mongodb - Adding date range condition in aggregation method

I am using Mongodb to get some matching records from the collection. I have a condition to get all the user who have mulitple records and having a sum greater than 2000. This code works fine. But i have to apply a date range to this matching condition.

db.CX_REMITTANCE.aggregate( [
   { "$group": { "_id": { "REMT_NAME": "$REMT_NAME" }, 
               "TRANS_REFNO": { "$push": "$TRANS_REFNO" },
               "SENDER_NAME": { "$push": "$REMT_NAME" },
               "REC_NAME": { "$push": "$BENF_NAME" },
               "count": { "$sum": 1 },//Count
               "TOTAL": { "$sum": "$LCYAMT" }//Sum
   }},
   { "$match": { "count": { "$gt": 1 }, TOTAL: { "$gt": 2000 } }}
])

Date Range to be applied where transdate between 20/02/2014 and 30/03/2014

    {"TRANSDATE" :{
        "$gte": ISODate("2014-02-200:00:00.000Z"),
        "$lt": ISODate("2014-03-300:00:00.000Z")
    }}

If i apply this condition on the $match i am not getting any result.Even the above result have records within this date range.

How can it be possible to add this condition.

Upvotes: 0

Views: 3242

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151180

Sounds a lot like your $match is in the wrong place:

db.CX_REMITTANCE.aggregate( [
    {" $match": {
        "TRANSDATE" :{
            "$gte": ISODate("2014-02-200:00:00.000Z"),
            "$lt": ISODate("2014-03-300:00:00.000Z")
        }
    }},
    { "$group": { "_id": { "REMT_NAME": "$REMT_NAME" }, 
        "TRANS_REFNO": { "$push": "$TRANS_REFNO" },
        "SENDER_NAME": { "$push": "$REMT_NAME" },
        "REC_NAME": { "$push": "$BENF_NAME" },
        "count": { "$sum": 1 },//Count
        "TOTAL": { "$sum": "$LCYAMT" }//Sum
    }},
    { "$match": { "count": { "$gt": 1 }, "TOTAL": { "$gt": 2000 } }}
])

Upvotes: 2

Related Questions