Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you query by Date and Date Range in Mongo

Tags:

php

mongodb

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.

like image 504
mabuza Avatar asked Sep 21 '11 23:09

mabuza


1 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 } });
like image 172
Gates VP Avatar answered Sep 22 '22 09:09

Gates VP