Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query MongoDb for a datetime value less than NOW

This question addressess generating a query from C#, but how does one generate a comparision value equivalent to GetDate() (SQL Server) in MongoDB

i.e. I would like to express a JSON query like this:

{
Expiration: {$lte:  Now())},
}
like image 536
Matt Evans Avatar asked Apr 22 '15 07:04

Matt Evans


People also ask

What is ISODate in MongoDB?

ISODate() is a helper function that's built into to MongoDB and wraps the native JavaScript Date object. When you use the ISODate() constructor from the Mongo shell, it actually returns a JavaScript Date object.

What is $GTE in MongoDB?

$gte selects the documents where the value of the field is greater than or equal to (i.e. >= ) a specified value (e.g. value .) For most data types, comparison operators only perform comparisons on fields where the BSON type matches the query value's type.


1 Answers

Use the Javascript function new Date():

db.collection.find({ Expiration: { $lte: new Date() } })

The Mongo shell is just a Javascript shell, so you can, in principle, use any Javascript method.

like image 123
Pascal Bugnion Avatar answered Sep 21 '22 07:09

Pascal Bugnion