Which SchemaType in Mongoose is Best for Timestamp?

Go To StackoverFlow.com


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?

2012-04-04 06:51
by Liatz


Edit - 20 March 2016

Mongoose now support timestamps for collections.

Please consider the answer of @bobbyz below. Maybe this is what you are looking for.

Original answer

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.

Source: http://mongoosejs.com/docs/guide.html

2012-04-04 07:06
by drinchev
Thank you very much for the answer but what i'm trying to understand is the best way to query for returning all the records that have been updated in the last 5 minutes. Do you mean I should use : date : {$gt: ((Math.round((new Date()).getTime() / 1000))-300) - Liatz 2012-04-04 10:02
@user1103897 you can construct a Date object directly and use it with $gt like this:

var now = new Date();
var fiveminago = new Date(now.getTime() - 5*60*1000);

then query with {date : {$gt:fiveminago} - mpobrien 2012-04-12 19:09

Should probably be Date.now instead of Date.now() - Alexey Zabrodsky 2012-12-25 19:18
+1 elmigranto, Mongoose docs example uses Date.now http://mongoosejs.com/docs/guide.htm - Aaron 2013-01-19 04:29
Explanation: Date.now because Date.now is a function that will be run when you make objects. Date.now() is the date that your models.js was parsed. Ie, if you use Date.now() all your objects will have the same date, and that will be the date models.js was parsed - mikemaccana 2014-06-24 09:49


The current version of Mongoose (v4.x) has time stamping as a built-in option to a schema:

var mySchema = new mongoose.Schema( {name: String}, {timestamps: true} );

This option adds createdAt and updatedAt properties that are timestamped with a Date, and which does all the work for you. Any time you update the document, it updates the updatedAt property. Schema Timestamps Docs.

2015-12-21 15:11
by bobbyz


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

This means you need to update the date to "now" every time you save the object. Maybe you'll find this useful: Moongoose create-modified plugin

2013-03-12 06:17
by Fletch