timezone mongo shell?

989 views
Skip to first unread message

Michael Di Domenico

unread,
May 31, 2016, 3:56:25 PM5/31/16
to mongod...@googlegroups.com
i have several documents that have ISODate fields. The date is filled
in with UTC time, i am in Eastern Time.

My understanding is that, if i use perl to extract the documents for
2016-05-31T00:00:00 to 2016-05-31T23:59:59 using the DateTime module,
the module will convert the dates to UTC before passing them to mongo
for retrieval.

My question is, how can i perform that same function using the mongo shell?

If i do

db.collection.find({ date: { $gte: ISODate('2016-05-31T00:00:00Z'),
$lte: ISODate('2016-05-31T23:59:59Z') } })

the resulting document set is messing the edge cases for my localtime
zone, since EDT is shifted back 5 hrs.

I know that i can manually shift the ISODate entries by five hours and
get all the documents, but i'm curious if there's a way to have mongo
shell do this automatically for me

Chris Cunningham

unread,
Jun 14, 2016, 12:51:16 AM6/14/16
to mongodb-user
Michael,

I noticed that you have a "Z" in your original date which indicates UTC (aka Zulu) time.  You can have the documents automatically shift the ISODate by specifying the "time offset" as described in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#UTC).

For example:

`db.collection.find({ date: { $gte: ISODate('2016-05-31T00:00:00Z'), $lte: ISODate('2016-05-31T23:59:59Z') } })`

Would be changed to:

`db.collection.find({ date: { $gte: ISODate('2016-05-31T00:00:00+05:00'), $lte: ISODate('2016-05-31T23:59:59+05:00') } })`

If you need to manipulate dates with time zones, you may want to consider loading a helper into the `mongo` shell using the [`load()` command](https://docs.mongodb.com/manual/reference/method/load/) (or by adding JavaScript to your [`.mongorc.js` startup file](https://docs.mongodb.com/manual/mongo/#mongorc-js-file)).

For example, [Moment.js](http://momentjs.com/) and [Moment Timezone](http://momentjs.com/timezone/) should be compatible with the `mongo` shell:

load('/path/to/moment.min.js')
load('/path/to/moment.timezone.min.js')

Please let us know if you have any questions.

Regards,

Chris

Michael Di Domenico

unread,
Jun 16, 2016, 9:55:20 AM6/16/16
to mongod...@googlegroups.com
On Tue, Jun 14, 2016 at 12:51 AM, Chris Cunningham
<chris.cu...@mongodb.com> wrote:
> I noticed that you have a "Z" in your original date which indicates UTC (aka
> Zulu) time. You can have the documents automatically shift the ISODate by
> specifying the "time offset" as described in [ISO
> 8601](https://en.wikipedia.org/wiki/ISO_8601#UTC).
>
> For example:
>
> `db.collection.find({ date: { $gte: ISODate('2016-05-31T00:00:00Z'), $lte:
> ISODate('2016-05-31T23:59:59Z') } })`
>
> Would be changed to:
>
> `db.collection.find({ date: { $gte: ISODate('2016-05-31T00:00:00+05:00'),
> $lte: ISODate('2016-05-31T23:59:59+05:00') } })`

Thanks that works. I didn't know you could format the ISOdate queries
like this.
Reply all
Reply to author
Forward
0 new messages