Home » Php » php – How do you query by Date and Date Range in Mongo

php – How do you query by Date and Date Range in Mongo

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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 } });