Home > Mobile >  which is the better way to use Date in mongodb
which is the better way to use Date in mongodb

Time:09-21

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...

CodePudding user response:

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