Jack
Jack

Reputation: 629

How to do query with timezone settings in Mongodb

I'm trying to transfer my application database from Mysql to Mongodb, but I met a problem with datetime queries.

In Mysql, when there is a datetime or timestamp column, you can set specific timezone for each request using command:

SET time_zone = timezone;
// do queries here

Is there any kind of similar solutions for this kind of demands?

I know applications can do the job after retrieving data from mongodb, but what about using aggregation with $hour, $month or $day operators?

Upvotes: 9

Views: 24431

Answers (2)

Nick
Nick

Reputation: 1554

MongoDB stores all dates and times in UTC. This allows the timezone support and translation to be done application side instead of server side.

The best way to store dates and times is via the Javascript Date object. This will allow you to convert dates to and from Unix timestamps, using getTime() and Date(milliseconds).

It should be noted, that Javascript stores time in milliseconds since UNIX Epoch, whilst the standard UNIX timestamp is in seconds since Epoch.

Upvotes: 18

saad arshad
saad arshad

Reputation: 259

had a similar issue discussed here, https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/PodDGnWM09Q

also, this website also might come in handy for future refrencing :) http://www.querymongo.com/

Upvotes: 1

Related Questions