0

My document has attribute 'timestamp' and I want to get all older than n days. The following query works just fine in mongodb shell:

{timestamp: { $gt: Date.now() - 1000*60*60*24*?0 } }

But when putting it inside Query anotation like this:

public interface TestRepo extends MongoRepository<TestEntry, String> {

    @Query("{timestamp: { $gt: Date.now() - 1000*60*60*24*?0 } }")
    Stream<TestEntry> get(Integer days);
}

I get json parse exception

Caused by: com.mongodb.util.JSONParseException: 

    {timestamp: { $gt: Date.now() - 1000*60*60*24?0 } }
                       ^
        at com.mongodb.util.JSONParser.parse(JSON.java:231) ~[mongo-java-driver-3.8.2.jar:na]



...
3
  • I guess this happens because it requires JSON and you have a JavaScript call inside. You have to use parameter and bind your's call value to it. Commented Jan 18, 2019 at 10:16
  • I could do that, but if the client and the database are in different time zones, it won't work. Is there another way to get the mongo current timestamp? Commented Jan 18, 2019 at 10:23
  • I prefer having everything on my servers in UTC. By the way, what about stackoverflow.com/questions/46612203/… ? Commented Jan 18, 2019 at 10:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.