0

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?

3
  • Maybe helpful link for You: stackoverflow.com/questions/30569228/… Commented Jan 26, 2017 at 12:22
  • What is your java version ? Commented Jan 26, 2017 at 12:25
  • @Veeram java 8 and spring boot Commented Jan 26, 2017 at 12:29

1 Answer 1

1
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)
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks it works. Can I request another help? If yes, How do I easily subtract 24 hours form that date format?
You're welcome. Something like instant = instant.minus(1,ChronoUnit.DAYS);
@vikram how do I get currnet datetime like this format "2017-01-25T00:16:15.184Z" easily? is new ISO8601DateFormat() correct?
just use Instant instant = Instant.now() to get current date time
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); Take a look at Java 8 datetime api to know more.
|

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.