I needed to find all the records in mongo db within two date ranges using Mongo Driver[3.4.0] for Java.
Example: I have books Collection.
{
"_id" : ObjectId("5acb40d27d63b61cb002bafe"),
"title" : "WingsOfFire",
"pub-date" : ISODate("2013-10-02T00:00:00.000Z"),
"rel-date" : ISODate("2013-11-02T00:00:00.000Z")
}
Like above I have 100s of documents.
I need to find all records wherein pub-date > rel-date.
I am using Mongo DB version 3.2.6
I tried to use $expr operator but it seems to work with only Mongo 3.6+
Not able find cleaner solutions for above requirement.
Please clarify.
The MongoDB (prior to v3.4) shell command for your use case is:
Translating this command into Java you'll get:
Given a collection with these documents ...
.. the above Java code will print ...
... because this document's
pub-date
(2013-11-02T00:00:00.000Z) is after itsrel-date
(2013-10-02T00:00:00.000Z).Note: the
$where
operator is functionally equivalent but use of that operator comes with some limitations:You might want to try $where-Operator: