Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I'm using Mongoose, MongoDB, and Node.

I would like to define a schema where one of its fields is a date\timestamp.

I would like to use this field in order to return all of the records that have been updated in the last 5 minutes.

Due to the fact that in Mongoose I can't use the Timestamp() method I understand that my only option is to use the following Javascript method:

time : { type: Number, default: (new Date()).getTime() }

It's probably not the most efficient way for querying a humongous DB. I would really appreciate it if someone could share a more efficient way of implementing this.

Is there any way to implement this with Mongoose and be able to use a MongoDB timestamp?

1 Answer

0 votes
by (106k points)

You do not need any schema type in MongoDB for timestamp because Mongoose supports a Date type which is basically a timestamp:

time : { type : Date, default: Date.now }

With the above field definition, any time you save a document with an unset time field, Mongoose will fill in this field with the current time.

Related questions

0 votes
1 answer
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)
0 votes
2 answers
0 votes
2 answers
0 votes
2 answers

Browse Categories

...