Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb timestamp is very inaccurate - mongoose

I am using mongoose to store objects in mongodb, I record the createdAt date by using Date.now()

What I am finding is that the wrong time is being stored in the database. The difference is very sporadic and has been 7 days, 3 days and even 5 minutes off.

Just this morning I created an object at 8.00AM (GMT+10) AEST, yet the time in the database was 7 days earlier.

This is the database object:

{
  "_id": "554bdfaf797cb8e02753e06f",
  "description": "test d",
  "createdBy": "testuser",
  "key": "f1a593f4dd51e632388a1755e09a7b4dc0bc0e24ef8bcf5cf859ac759a45e8a6",
  "__v": 0,
  "files": [],
  "createdAt": "2015-05-01T05:42:07.687Z"
}

I have seen this issue on both on my mac and win 7.

First noticed on mongodb version 2.6.3, just upgraded to 3.0.2, no change.

Update

I am setting the createdAt date within the schema like so:

var uploadSchema = new Schema({
    createdAt: {
        type: Date,
        required: true,
        default: Date.now()
     },

My database and application are running on the same host.

gist here - https://gist.github.com/reecefenwick/1bcff85d18406b33e5cf

like image 577
Reece Avatar asked May 07 '15 22:05

Reece


People also ask

What is timestamps true in mongoose?

Timestamps save the current time of the document created and also when it was updated in form of a Date by turning it true. When set to true, the mongoose creates two fields as follows: createdAt: Date representing when the document was created. updatedAt: Date representing when this document was last updated.

Is Mongoose good for MongoDB?

Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.

What format are MongoDB timestamps?

Timestamps. BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. This internal timestamp type is a 64 bit value where: the most significant 32 bits are a time_t value (seconds since the Unix epoch)

How is Timestamp stored in MongoDB?

How does Timestamp work in MongoDB? Working of the timestamp in mongodb is simple, where when executed, the timestamp method will call the currentDate(), which will pick the current date and time of the system. This picked date and time will be stored in the collection, along with the other data values.


1 Answers

What's going on is that you're calling Date.now() at the time the schema is defined to set the default value for createdAt, and then that value is being used for the default until the next time your app is restarted.

Instead, you want to set the default value to the Date.now function itself so that it will be called each time a new doc is created:

var uploadSchema = new Schema({
    createdAt: {
        type: Date,
        required: true,
        default: Date.now
     },
like image 58
JohnnyHK Avatar answered Sep 20 '22 07:09

JohnnyHK