Yang
Yang

Reputation: 49

Can't query date from MongoDB(Mlab) -- So simple but can't be solve

After weeks of effort, I'm still having trouble querying the date field movie_datetime which has the structure below from Mlab using NodeJS.

"movie_datetime": {
        "$date": "2017-01-03T16:00:00.000Z"
},
"session_id": 31268

enter image description here

I tried the following

db.mycollection.find({
  "movie_datetime" : {"gte" : { "$date" : "2013-10-01T00:00:00.000Z"}}
})

db.mycollection.find({
    "movie_datetime" : {"$gte": new Date("2013-10-01T00:00:00.000Z")}
})

db.mycollection.find({
    "movie_datetime" : {"$gte": Date("2013-10-01T00:00:00.000Z")}
})

db.mycollection.find({
    "movie_datetime" : {"$gte": ISODate("2013-10-01T00:00:00.000Z")}
})

Appreciate any help.

Current nodeJS Codes :

**Tried variations of single quotes and doubles quotes and omitting .toISOString

var dateTime = '{"movie_datetime":{"$gte" : "'+new Date("2017-01-03T16:00:00.000Z").toISOString()+'"}}';

var dateTimeJson =JSON.parse(dateTimeVar);

db.mycollection.find(dateTimeJson);


Result for print dateTime 
{"movie_datetime":{"$gte" : "2017-01-03T16:00:00.000Z"}}

Result for print dateTimeJson 
{ movie_datetime: { '$gte': '2017-01-03T16:00:00.000Z' } }

Node version 6.6.0

Upvotes: 3

Views: 2831

Answers (3)

Richard
Richard

Reputation: 51

If you are using the mlab interface, it's actually like this:

{
    "createdAt": {
        "$gt": {
            "$date": "2017-11-08T20:05:45.866Z"
        }
    }
}

Upvotes: 4

tsukyonomi06
tsukyonomi06

Reputation: 579

Check the docs

Dates are inserted in format given below

{"myDate": {"$date": "2010-07-20T23:23:50Z"}}

Upvotes: 0

QuasiFigures
QuasiFigures

Reputation: 67

I think this should do it.

db.myCollection.find({
  'movie_datetime.$date': {
    '$gte': new Date('2013-10-01T00:00:00.000Z')
  } 
});

edit - The picture you posted and the JSON you provided are different. For the picture it would be -

db.myCollection.find({
  'date.$date': {
    '$gte': new Date('2013-10-01T00:00:00.000Z')
  } 
});

Upvotes: 0

Related Questions