java Aggregation.match date

342 views
Skip to first unread message

forough m

unread,
Feb 13, 2018, 5:10:00 AM2/13/18
to mongodb-user
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?

Wan Bachtiar

unread,
Feb 21, 2018, 2:25:06 AM2/21/18
to mongodb-user

How can i correct it?

Hi,

I’ve tested the example aggregation above using MongoDB Java Driver v3.6:

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.

forough m

unread,
Feb 21, 2018, 6:57:07 AM2/21/18
to mongod...@googlegroups.com
Thank you :) Yes, i use spring data MongoDB. 
I understood that spring data convert my dateTime to UTC time zone. My system timezone is different from UTC, so spring data convert real dateTime to 3:30 hours backward.
To solve this problem, before passing dateTime to spring data, i added 3:30 hour to my dateTime and this work.
But this not good solution. I am searching better solution.

--
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.

Wan Bachtiar

unread,
Feb 26, 2018, 2:14:31 AM2/26/18
to mongodb-user

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.

forough m

unread,
Feb 27, 2018, 12:29:47 AM2/27/18
to mongodb-user
Good. thank you :)
Reply all
Reply to author
Forward
0 new messages