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.
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'm trying to test that the time.Time value I inserted into a postgres database is the same one I am querying out. Postgres drops the timezone though, so I'm wondering how can I get this testify/assert test to pass?
s.Equal(want.Date, got.Date)
Both are the dame datatype time.Time but the first has the timezone:
2020-10-31 00:00:00 +0000 UTC
I created this value like this - time.Date() must take a location, so I couldn't create it without one by passing nil:
want.Date := time.Date(2020, 10, 31, 00, 00, 00, 0000, time.UTC)
got.Date coming from the database looks like:
2020-10-31 00:00:00 +0000 +0000
I can't change the database dropping the timezone, so how can I change how I create the date or how can I drop the timezone? Or perhaps there is another way I can assert that the year, month, and day, are the same?
Use time.Time.Equal to test if two values of time.Time type are equal or not.
$ go doc time.Time.Equal
package time // import "time"
func (t Time) Equal(u Time) bool
Equal reports whether t and u represent the same time instant. Two times can
be equal even if they are in different locations. For example, 6:00 +0200
and 4:00 UTC are Equal. See the documentation on the Time type for the
pitfalls of using == with Time values; most code should use Equal instead.
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
I'm looking for a small script to update existing dates in a mongo database and change them to UTC without changing the dates. I had been adding documents into a MongoDb collection with some timeless date properties that, without intending it, included the local timezone. Now that I've caught my mistake, I'm setting their timezone to UTC in the client before being submitted: but, I want to fix the documents I added earlier. Would you provide a sample update script that magically fixes the currents dates I have entered: the properties in UTC should not be affected; the ones with EST or EDT timezones should become UTC dates without changing their dates.
Well, first thing is that Mongo dates are UTC, but still there were hours on the dates. To remove the dates, I did this:
db.coll.find({$where: "this.dt && this.dt.getUTCHours() > 0"}
).forEach(function(doc) {
db.coll.update({ _id : doc._id },
{ $set : { dt : new Date(doc.dt.getTime() -
(doc.dt.getUTCHours() * 60 * 60 * 1000))}});}
);
And this is a slightly modified copy of this:
https://stackoverflow.com/a/18773804/507421
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