2

I have a collection as below

{
    "Email" : "[email protected]",
    "start" : ISODate("2015-11-09T12:00:00.000-06:30")
},


{
    "Email" : "[email protected]",
    "start" : ISODate("2015-11-09T12:00:00.000-06:30")
},

{
    "Email" : "[email protected]",
    "start" : ISODate("2015-08-03T12:00:00.000-06:30")
},

{
    "Email" : "[email protected]",
    "start" : ISODate("2016-01-15T12:00:00.000-06:30")
}

I want fetch all the emails that are 90 days old from today's date but doesn't exists in last 30 days.So in the above collection below 2 documents are 90 days old

{
    "Email" : "[email protected]",
    "start" : ISODate("2015-11-09T12:00:00.000-06:30")
}


{
    "Email" : "[email protected]",
    "start" : ISODate("2015-11-09T12:00:00.000-06:30")
}

However "Email" : "[email protected]" exists in last 30 days (see last document above), so the output should be

{
    "Email" : "[email protected]",
    "start" : ISODate("2015-11-09T12:00:00.000-06:30")
}
4
  • Why is "[email protected]" not in the result?
    – Sede
    Feb 8, 2016 at 8:04
  • Because it is greater than 90 days. I am looking for exact 90 days
    – Ravi A.
    Feb 8, 2016 at 9:06
  • If you want exactly 90 days, none of your documents matches.
    – Sede
    Feb 8, 2016 at 9:08
  • Edited dates.I am querying on records that are output for below query db.testing.find({'start' :{'$gte': new Date(ISODate().getTime()-91*24*60*60*1000),$lte:new Date(ISODate().getTime()-90*24*60*60*1000) }},{_id:0,Email:1,start:1})
    – Ravi A.
    Feb 8, 2016 at 9:36

1 Answer 1

1

Here is the aggregation query to achieve this:

The match condition is (find all docs with start date 90 days old and not present in the last 30 days).

db.Testing.aggregate([
    {'$group': {
        '_id': '$Email',
        'start': {'$addToSet' : '$start'}}}, //Consolidating docs
    {'$match' : {
        '$and': [
            {'start' :{'$not': {'$gte':  new Date(ISODate().getTime()-30*24*60*60*1000) }}}, //Removing docs where the date exists in the last 30 days.
            {'start' :{'$eq':  new Date(ISODate().getTime()-90*24*60*60*1000) }}
        ]}}
])

I am assuming you want docs that are exactly 90 days old. If it's for document that are older than 90 days just replace the $eq to with $lte.

7
  • {'start' :{'$eq': new Date(ISODate().getTime()-90*24*60*60*1000) }} in the match will not work for me as I do have time stamp in the start field so I tried to replace that condition with something like this 'start' :{'$gte': new Date(ISODate().getTime()-91*24*60*60*1000),$lte:new Date(ISODate().getTime()-90*24*60*60*1000) } but even that is not working.
    – Ravi A.
    Feb 8, 2016 at 9:19
  • It's working on my side. btw the difference between 2015-11-08 and today's date is not 90 days. Feb 8, 2016 at 9:34
  • Ya I have records in DB for the entire 11/2015 so shouldn't be a problem still not working as timestamp is there and $eq is trying to match exact timestamp
    – Ravi A.
    Feb 8, 2016 at 9:52
  • No I meant the change you suggested is working on my setup. Maybe try giving partial condition i.e. only gte then only lte to check what exactly is wrong. Feb 8, 2016 at 9:55
  • 1
    I used a different version of this and it fulfilled my purpose.Thanks.
    – Ravi A.
    Feb 8, 2016 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.