Date startDate = new Date(System.currentTimeMillis() - 3600 * 1000);
Date endDate = new Date();
I have something like above. I use them in :
MatchOperation matchStage = Aggregation.match(new Criteria().where("dateTime")
.gte(startDate).lt(endDate).and("type").is(matchType));
But matchStage does not show correct startDate. It fill with false hour. I think it replaces startDate.toInstant().
How can i correct it?
Date startDate = new Date(System.currentTimeMillis() - 3600 * 1000);
Date endDate = new Date();
List pipeline = asList(
Aggregates.match(Filters.and(Filters.gte("datetime", startDate), Filters.lt("datetime", endDate)))
);
System.out.println(pipeline);
The output of the pipeline filter returns the correct hour value.
[Stage{name='$match', value=And Filter{filters=[Operator Filter{fieldName='datetime', operator='$gte', value=Wed Feb 21 06:10:08 UTC 2018}, Operator Filter{fieldName='datetime', operator='$lt', value=Wed Feb 21 07:10:08 UTC 2018}]}}]
It’s not clear from your post, but it looks like you are using Spring Data MongoDB ?
Have you tried adding the criteria with AND , as below ?
new Criteria().where("dateTime").gte(startDate).and("dateTime").lt(endDate)
If you have further questions relating to Spring Data MongoDB usage, I would recommend to post on StackOverflow.com with tags : tag:spring-data and tag:spring-data-mongodb to reach wider audience.
Regards,
Wan.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/GFudtEqJNzI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/493a7ae8-23e2-49b4-b6fd-fdbe40600306%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To solve this problem, before passing dateTime to spring data, i added 3:30 hour to my dateTime and this work.
Hi,
Please avoid performing manual date manipulation to deal with timezone. i.e. take into consideration of daylight savings and other timezone users for example.
MongoDB stores times in UTC by default, and will convert any local time representations into UTC.
Your application may store the time zone alongside the UTC timestamp, and compute the original local time in your application logic.
See also Tutorial: Model Time Data.
Worth mentioning that since MongoDB v3.6, aggregation operators for date also support timezone. i.e. $hour with optional timezone parameter.
Regards,
Wan.