3

I have 2 fields like this in my document

"DATE_1" : ISODate("2017-08-11T04:00:00Z")
"DATE_2" : ISODate("2017-06-12T04:00:00Z")

I would like to select documents with "DATE_1" is greater than "DATE_2" and i tried the following query which should return the above document.But its not giving any result

db.collection.find({"DATE_1":{$gte:"DATE_2"}})

How do I compare 2 ISODate fields in Mongo? Also how do I make this query using Spring date mongodb? something like this?

Criteria.where("DATE_1").gte("DATE_2");
user3528213
  • 1,415
  • 3
  • 15
  • 18
  • Nope, you can't make such query. Not in the regular querying language, anyway. Your only hope is `$where` (slow) and, maybe, aggregation pipeline – Sergio Tulentsev Jun 07 '17 at 15:41

1 Answers1

3

This should work. Can you please try this and give your feedback.

db.getCollection('collectionName').find({$where: function() { 
    return this.DATE_1 > this.DATE_2;
}})
Morten Siebuhr
  • 6,068
  • 4
  • 31
  • 43
Udara Gunathilake
  • 202
  • 1
  • 2
  • 14