which is the better way to use Date in mongodb

Viewed 12

we can store new Date() to database or also we can use mongodb ObjectId to get date using objectId.getTimestamp() My question is which is the efficient way to store and retrieve Date

Tutorialspoint.com says: Duplicate the data (but limited) because disk space is cheap as compare to compute time.

So which approach is better...

1 Answers

It depends on your requirements.

First ObjectId resolves only up to second, whereas Date object has resolution of Millisecond.

The _id field is created automatically and you cannot modify it. So if you need just an "inserted at ..." field, then _id: ObjectId(...) might be sufficient.

Related