Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which SchemaType in Mongoose is Best for Timestamp?

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?

like image 205
Liatz Avatar asked Apr 04 '12 06:04

Liatz


People also ask

What is timestamps in Mongoose schema?

Mongoose schemas support a timestamps option. If you set timestamps: true , Mongoose will add two properties of type Date to your schema: createdAt : a date representing when this document was created. updatedAt : a date representing when this document was last updated.

What is schema type in Mongoose?

What is a SchemaType? You can think of a Mongoose schema as the configuration object for a Mongoose model. A SchemaType is then a configuration object for an individual property. A SchemaType says what type a given path should have, whether it has any getters/setters, and what values are valid for that path.

What is the type of Date in Mongoose?

MongoDB stores dates as 64-bit integers, which means that Mongoose does not store timezone information by default. When you call Date#toString() , the JavaScript runtime will use your OS' timezone.

What is timestamp 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.


2 Answers

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

like image 84
drinchev Avatar answered Oct 19 '22 11:10

drinchev


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.

like image 30
bobbyz Avatar answered Oct 19 '22 11:10

bobbyz