Januka samaranyake
Januka samaranyake

Reputation: 2597

Query by date in mongodb using java

I was going to get distinct values form collection. I stored time as follows:

"time" : ISODate("2017-01-26T09:46:26.523Z")

new ISO8601DateFormat() is not working, that gives me below error

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.fasterxml.jackson.databind.util.ISO8601DateFormat.

My code is looks like below.

Query query = new Query();
query.addCriteria(Criteria.where("user_id").is(id).and("time").gt(new ISO8601DateFormat()));
mongoTemplate.getCollection("user_log").distinct("timezone", query.getQueryObject())

My mongodb terminal command is follows and it works perfectly.

db.user_log.find({ "user_id" : "1" , "time" : { "$gt" : new ISODate("2017-01-25T00:16:15.184Z")}})

What is correct way to approach when I access from java?

Upvotes: 0

Views: 3875

Answers (1)

s7vr
s7vr

Reputation: 75984

Instant instant = Instant.parse("2017-01-25T00:16:15.184Z"); 
Date time = Date.from(instant);

Replace your time criteria with below

and("time").gt(time)

Upvotes: 1

Related Questions