'Inconsistent Date field in mongodb , why happen , how to identify and fix
I have identified recently date fields in my mongod 4.0 with such a content:
"last_update" : ISODate("-229-05-06T07:23:23Z")
"last_update" : ISODate("50170-12-13T06:03:34Z")
"last_update" : ISODate("0000-07-23T05:19:55Z")
So my question:
- Is mongodb allowing such freedom for date fields ( signed 64bit integer of type date ) ?
According to this :
https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#conversion-table
It seems like the format is OK?
Datetime [year before 1970 or after 9999] {"$date": {"$numberLong": <64-bit signed integer giving millisecs relative to the epoch, as a string>}}
Attempts to insert it from mongo shell ISO date helper as expected did not allowed me:
MongoDB Enterprise mongos> db.test.insert({ "created" : ISODate("-229-05-06T07:23:23Z") })
2021-01-29T11:47:53.484+0100 E QUERY [js] Error: invalid ISO date: -229-05-06T07:23:23Z :
ISODate@src/mongo/shell/types.js:65:1
@(shell):1:31
MongoDB Enterprise mongos>
But at the same time insert affected fields from the original document in another collection was not having issues:
MongoDB Enterprise mongos> var x = db.theAffectedCollection.findOne({_id:ObjectId("5c6e8c6ce0ebbb309ce0dc06")},
{created:1,last_update:1})
MongoDB Enterprise mongos> use test
MongoDB Enterprise mongos> db.test.insert(x)
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise mongos> db.test.find()
{ "_id" : ObjectId("5c6e8c6ce0ebbb309ce0dc06"), "created" : ISODate("-229-05-06T07:23:23Z"), "last_update" : ISODate("-229-05-06T07:23:23Z") }
mongoexport result:
"created":{"$date":"-0229-05-06T07:23:23.000Z"}
Luckily the document _id timestamp show the exact creation date:
ObjectId("5c6e8c6ce0ebbb309ce0dc06").getTimestamp()
ISODate("2019-02-21T11:33:00Z")
So I could easily fix it ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
