Search code examples
javamongodbmongodb-querymongodb-java

Mongodb Java query for date range


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.


Solution

  • You might want to try $where-Operator:

    db.books.find({ "$where": "this.pub-date > this.rel-date"});