简体   繁体   中英

How do you query by Date and Date Range in Mongo

This would be the equivalent of this SQL statement?

Select * from example WHERE date = '2011-09-21'

The record is stored with a MongoDate field.

I would also like to know the syntax of the between query.

This would be the equivalent of this SQL statement?

Select * from example WHERE date = '2011-09-21'

db.example.find({date: dateobject});

In the case of MongoDB + PHP, you'll want to use the [MongoDate][2] class to represent those dates. Other language drivers typically just use the language date construct.

I would also like to know the syntax of the between query.

MongoDB does not have a between clause.

To use "Greater than" you will need to use one of the query operators. See here for details. Simple example:

db.example.find({ date: { $gt: lowdate, $lt: highdate } });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM