I am storing dates in Mongo DB using Mongoose, they should be in UTC, but I am seeing this using a Mongo IDE (Studio3T) - I take it that the +0000 does not mean UTC?
"eventDate" : ISODate("2019-12-27T17:01:00.000+0000"),
+0000 means UTC according to ISO 8601. The letter Z is also possible to indicate UTC time.
Yes,Dude! By default the dates are stored in UTC format in MongoDB.
For further information,head over to below links.
https://docs.mongodb.com/manual/tutorial/model-time-data/
MongoDb BSON stores Date in UTC time
Related
When I look at all the documents in Compass, I can see all the dates are UTC as expected. But when I used Mongo-Java-Driver and read from the db using MongoCollection -> MongoCursor - all the Date objects in my Document object is in my local timezone (PDT)
Does anyone know how to keep the date in UTC format when I do my read
I am trying to convert UTC time to Local Timezone. I only require the date to be extracted. When I try the below it works and gives correct results.
select to_date(FROM_UTC_TIMESTAMP(current_timestamp(), 'Pacific/Fiji'));
But if I try, short ID of the respective timezone it is giving wrong results.
select to_date(FROM_UTC_TIMESTAMP(current_timestamp(), 'FJT'));
The issue was with the difference in GMT and UTC short IDs. In the above example 'Pacific/Fiji' is a UTC based short ID where are 'FJT' is GMT equivalent. Since we are using hive function FROM_UTC_TIMESTAMP we have to use UTC based short IDs. Here is a list of UTC based short IDs
I have a Mongo collection with 'DateAdded' in ISODate format
{
"_id" : 4098199,
"DateAdded" : ISODate("2018-08-31T05:06:13.150Z")
}
so it is 1hour off then local datetime
LocalDatetime is 2018-08-31 06:06:13 +01:00
UTC is 2018-08-31T05:06:13.150Z
Using following query when i try to get records added after 2018-08-31 06:00:00 i m not getting this records as it is in UTC
db.getCollection('User').find({"DateAdded":{$gte: new ISODate('2018-08-31T6:00:00Z')}})
How to convert UTC date while doing search on date field in MongoDB?
First of all ISODate('2018-08-31T6:00:00Z') has a typo - it should be 06:00:00Z. Without leading 0 ISODate helper returns '2018-08-31T00:00:00Z' 0am UTC and the query actually returns the document.
Secondly, there is nothing about local time. ISODate is in UTC, so
ISODate("2018-08-31T05:06:13.150Z") is 5am UTC and ISODate("2018-08-31T06:06:13.150Z") is 6am UTC. 5 < 6, so the $gte condition doesn't meet and the document is not returned.
Lastly, if you want to use local time - use Date instead:
db.getCollection('User').find({"DateAdded":{$gte: new Date('2018-08-31T6:00:00')}})
Will return all documents created after 5am UTC, 6am local time. Note, there is no Z at the end, which specifies UTC timezone.
As a recommendation - don't mess up with local time. Convert it to UTC as soon as possible and always work with UTC.
By Default the mongodb stores the time in default UTC format Mongo Doc
while executing
new Date() //ISODate("2016-10-05T07:54:21.525Z")
I want the result in IST timezone
If there is any settings to change into IST or any other timezone
NO, you can not change mongodb timezone format from UTC to IST.
What you can do is just create a function/middleware to convert UTC to IST and use this module wherever you need to display time in IST.
When I get the date object and print it in mongo shell, it display two different time as follows:
>new Date()
Mon Feb 06 2012 18:49:40 GMT+0530 (IST)
>printjson({created_at: new Date()})
{ created_at : ISODate("2012-02-06T13:19:40.313Z") }
The two times are different, what i am wrong.
This is because of mongodb always store dates in UTC format, but javascript show your local time. And printjson internal mongodb shell function that convert date from your local timezone to utc format. So it shows -5.30 hours backward from your current time.
The times are not different, they are exactly the same! It's merely the same time expressed in different timezones. The "Z" in ISODate means "UTC" (or as some people try to call it: GMT). You're on IST (Indian Standard Time I guess) which is at GMT+0530. 18:49:40 # GMT+5:30 is exactly the same as 13:19 # GMT.
When I run the same code you shown on the shell, I get:
> new Date()
ISODate("2012-02-06T13:34:10.667Z")
As you can see, that is also with "Z". Perhaps you're running an older version of the shell?
cheers,
Derick