0

I want to query a mongo collection to fetch documents created before a given date. I use the MongoRepository query findByDateBefore(). But turns out, it doesn't return any records. Here's my code implementation (pseudo): MyDocument model

@Document(collection = "mydocument")
public class MyDocument {
   @Id
   String name;
   Date creationDate;
}

The repository:

@Repository
public interface MyDocumentRepository extends MongoRepository<MyDocument, String> {
   List<MyDocument> findByCreationDateBefore(Date date);

   // @Query("{'creationDate': {"$lt": ?0}}")
   // List<MyDocument> findbyCreationDateBefore(Date date)
}

Service code:

java.util.Date today = new Date();
List<MyDocument> documents = myDocumentRepository.findByCreationDateBefore(today);

The query logged after the service call:

MongoTemplate: find using query: { "creationDate" : { "$lt" : { "$date" : "2020-03-12T13:17:23.784Z"}}} fields: null for class: class com.myrepo.model.MyDocument in collection: mydocument

This return no results. I have tried using custom query (commented code in repository) which also doesn't return any records. Also I tried running the logged query in the mongo console and it does not return any results: { "creationDate" : { "$lt" : { "$date" : "2020-03-12T13:17:23.784Z"}}}
But when I use new Date, in mongo console, it works fine. e.g.: { "creationDate" : { "$lt" : new Date("2020-03-12T13:17:23.784Z")}}.

Solutions mentioned here doesn't work for me.

Avdhut Mankavale
  • 395
  • 3
  • 12
  • Hey there, Avdhut. Hope everything's going well for you. We've got this working in Spring Data Mongo, and we use `Instant` instead of `java.util.Date` and it works. Is that an option? – Christopher Schneider Mar 12 '20 at 19:45
  • @ChristopherSchneider Thank you Chris for the answer! I tried your solution of using Instant in the repository method, it still doesn't return anything. Here's the logged query `query: { "creationDate" : { "$lt" : { "$date" : "2020-03-12T15:12:01.222Z"}}} fields: null`. I don't understand the `fields:null` part of it, not sure where the fields should be configured. – Avdhut Mankavale Mar 12 '20 at 20:15
  • 1
    Ah, that format's still not correct. That's not the same behavior I'm seeing. Not sure if there's a custom converter or perhaps a newer version of Spring Data Mongo (2.2.1), but I think you've got two choices: 1. Use `MongoTemplate` directly. 2. Write a custom converter. See [this question](https://stackoverflow.com/questions/23972002/java-8-date-time-jsr-310-types-mapping-with-spring-data-mongodb) for some potential solutions. – Christopher Schneider Mar 12 '20 at 21:13
  • See if this is useful: [Why spring data mongo not returning the field having time?](https://stackoverflow.com/questions/60083930/why-spring-data-mongo-not-returning-the-field-having-time). – prasad_ Mar 13 '20 at 02:03
  • @ChristopherSchneider using the MongoTemplate worked for me. I had to create a custom implementation of the repository. Thank you! – Avdhut Mankavale Mar 18 '20 at 13:41

0 Answers0