Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

node.js - MongoDB + nodejs : how to query ISODate fields?

I am using nodejs with the node-mongodb-native driver (http://mongodb.github.io/node-mongodb-native/).

I have documents with a date property stored as ISODate type.

Through nodejs, I am using this query:

db.collection("log").find({
    localHitDate: { 
            '$gte': '2013-12-12T16:00:00.000Z',
            '$lt': '2013-12-12T18:00:00.000Z' 
    }
})

It returns nothing. To make it work I need to do the following instead:

db.collection("log").find({
    localHitDate: {
            '$gte': ISODate('2013-12-12T16:00:00.000Z'),
            '$lt': ISODate('2013-12-12T18:00:00.000Z')
    }
})

But ISODate is not recognized in my nodejs code.

So how can I make a query against mongo date fields through my nodejs program?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use new Date('2013-12-12T16:00:00.000Z') in node.js;

new is a must, because Date() is already use to return date string.

ISODate is concepted in mongodb, you can use it in mongodb console, but it can be different for different programming language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...