0

My question is an extension of this one MongoDB/Mongoose querying at a specific date?

I have a model Tournament. They will have games on multiple dates. I want to query for tournaments that have games on a certain date. I tried to used the method described in the post.

Example data

{
    "_id": {
        "$oid": "59a70655b300012a044b2a48"
    },
    ...,
    "gameDates": [
        {
            "$date": "2017-09-01T05:59:59.000Z"
        },
        {
            "$date": "2017-09-02T05:59:59.000Z"
        }
    ]
}

Using mlab, this returns nothing. If I query for specific $date, not a range, it works in mlab, not mongoose.

{
    "gameDates": {
        "$date": {
            "$lt": "2017-09-02T00:00:00.000Z"
        }
    }
}

Update: In node,

var conditions={gameDates: { $date: '2017-09-01T05:59:59.000Z' } };
var results='gameDates';
Tournament.find(conditions, results).exec(
    function(err, tournament) {
        if (err) {
            console.log("err"+err);
        }
        console.log("tournament",tournament);
});

Gives me

errError: Can't use $date with Array.

rrayas
  • 43
  • 8
  • 1
    What is the actual code? All you are showing here is the serialization, and that is not actually valid to issue as a query. You should be issuing something like `.find({ "gameDates": { "$lt": new Date("2017-09-02") } })` which should be a valid match since the first array element meets that criteria. It's also generally best to show documents as they appear from the mongo shell, as this gives a true indication of how the data is actually stored. – Neil Lunn Aug 31 '17 at 01:03
  • Didn't see the answer was right there – rrayas Aug 31 '17 at 03:38

0 Answers0