Reputation: 2245
I have a collection wit documents that have a Date field, I need to find the document with the latest date that is also before a specific date I provide. Say I have documents with the following dates
2000, 2003, 2004, 2006, 2010, 2011
I need to find a document that is the max date but prior to a date I provide, for example db.collection.find(2005) -> will return 2004 db.collection.find(2000) -> will return null db.collection.find(2015) -> will return 2011
Upvotes: 0
Views: 36
Reputation: 1487
Assuming dates are stored with field name: "date", we can get the required result using the below query -
db.collection.find({date:{$lt:2004}}).sort({date:-1}).limit(1)
returns :
Upvotes: 2