Garfield
Garfield

Reputation: 2537

Array of dates matches my date range

I have the documents like below

{
"dates":[ISODate("2014-12-08T18:05:14.178Z"),
        ISODate("2014-12-07T18:05:14.178Z"),
        ISODate("2014-12-06T18:05:14.178Z")]
}, 
{
"dates":[ISODate("2014-12-01T18:05:14.178Z"),
        ISODate("2014-12-02T18:05:14.178Z"),
        ISODate("2014-12-06T18:05:14.178Z")]
}, ...

I wanted to filter the collections which has the dates containing a date ranging between

ISODate("2014-12-08T00:00:00.000Z") and ISODate("2014-12-08T23:59:59.000Z")

And the result should be

[{
    "dates":[ISODate("2014-12-08T18:05:14.178Z"),
            ISODate("2014-12-07T18:05:14.178Z"),
            ISODate("2014-12-06T18:05:14.178Z")]
    }]

I just have an idea like trying $in operator but how does it filters the date range?

I am very new to mongoDB.

EDIT

db.crawler_status.find({dates:{$gt:new ISODate("2015-02-04 00:00:00:000Z"), $lt: new ISODate("2015-02-04 23:59:59:000Z")}});

Result:

{
    ...
    "dates" : [ 
        ISODate("2014-12-05T00:28:13.584Z"), 
        ISODate("2014-12-05T03:47:10.841Z"), 
        ISODate("2014-12-05T04:27:53.166Z"), 
        ISODate("2014-12-05T08:28:13.476Z"), 
        ISODate("2014-12-05T10:18:09.281Z")
    ]
}

/* 1 */
{
    "check_dates" : [ 
        ISODate("2014-12-05T03:24:04.651Z"), 
        ISODate("2014-12-05T03:39:45.596Z"), 
        ISODate("2014-12-05T07:40:05.435Z"), 
        ISODate("2014-12-05T11:40:06.114Z"), 
        ISODate("2014-12-05T15:40:05.842Z")
    ]
}....

Upvotes: 1

Views: 1587

Answers (2)

Nitin Manocha
Nitin Manocha

Reputation: 140

You can use $elemMatch on the array of Dates:

db.collection.find({ 
  "dateArray": {
    "$elemMatch": {  "$gte": startDate, "$lte": endDate  }
  }
})

Upvotes: 0

Neo-coder
Neo-coder

Reputation: 7840

If your mongo documents as below

{
    "_id" : ObjectId("54d35b0457941c5a55ff3bf4"),
    "datesData" : [
        {
            "dates" : [
                ISODate("2014-12-08T18:05:14.178Z"),
                ISODate("2014-12-07T18:05:14.178Z"),
                ISODate("2014-12-06T18:05:14.178Z")
            ]
        },
        {
            "dates" : [
                ISODate("2014-12-01T18:05:14.178Z"),
                ISODate("2014-12-02T18:05:14.178Z"),
                ISODate("2014-12-06T18:05:14.178Z")
            ]
        }
    ]
}

Then below aggregation query shows the result which you want

db.collectionName.aggregate({
    "$unwind": "$datesData"
},
{
    "$match": {
    "$and": [
        {
            "datesData.dates": {
                "$gt": ISODate("2014-12-08T00:00:00.000Z")
            }
        },
        {
            "datesData.dates": {
                "$lt": ISODate("2014-12-08T23:59:59.000Z")
            }
        }
    ]
    }
}).pretty()

Or if your collections structure as below

{
    "_id" : ObjectId("54d35eca57941c5a55ff3bf5"),
    "dates" : [
        ISODate("2014-12-08T18:05:14.178Z"),
        ISODate("2014-12-07T18:05:14.178Z"),
        ISODate("2014-12-06T18:05:14.178Z")
    ]
}
{
    "_id" : ObjectId("54d35ed657941c5a55ff3bf6"),
    "dates" : [
        ISODate("2014-12-01T18:05:14.178Z"),
        ISODate("2014-12-02T18:05:14.178Z"),
        ISODate("2014-12-06T18:05:14.178Z")
    ]
}

Then try this simple query

db.collectionName.find({
    "$and": [
    {
        "dates": {
            "$gt": ISODate("2014-12-08T00:00:00.000Z")
        }
    },
    {
        "dates": {
            "$lt": ISODate("2014-12-08T23:59:59.000Z")
        }
    }
    ]
}).pretty()

Upvotes: 3

Related Questions