MongoTemplate and ISODate

When you query from shell you are probably using something like this:

Consider these mongo shell queries:

db.dates.find( { dt: { $gte: ISODate("2020-09-22") } } )
returns: { "_id" : 1, "dt" : ISODate("2020-09-23T12:10:15.710Z") }

db.dates.find( { dt: { $gte: new Date("2020-09-22") } } )
returns: { "_id" : 1, "dt" : ISODate("2020-09-23T12:10:15.710Z") }

The ISODate() (and new Date()) used above are date objects used in shell (see Date()). Similarly, in your Java code you use the Java representation of date with your queries. And, it is to the java.util.Date or java.time.LocalDateTime (NOTE: I had already posted Java code in my earlier comments an example query using java.util.Date).

About the Query Performance:

The query performance in Java application depends upon the code, data, its representation in Java, the amount of data, the environment (hardware and software) it is being run, and other factors like operations on the system when you are running the query.

You can run an explain on the query and post the query plan here. It can be analyzed. Do include the Java code used, sample document from the collection, its Java representation (typically a POJO class), info about amount of data, and the environment it is run.

1 Like