1.0.0 • Published 10 years ago
mongoose-minute v1.0.0
mongoose-minute
Mongoose 4.0 plugin for setting created and updated timestamps.
npm install mongoose-minuteUsage
By default two additional properties are added to the schema createdAt and updatedAt. The timestamps are updated for each call to save, update and findOneAndUpdate. The module uses hooks only available in Mongoose 4.0, and the MongoDB $setOnInsert operator.
Pass the schema to the module.
var mongoose = require('mongoose');
var minute = require('mongoose-minute');
var schema = new mongoose.Schema({
counter: { type: Number, default: 0 }
});
minute(schema);
var Model = db.model('Model', schema);
// Set createdAt and updatedAt on model instance
var model = new Model();
model.save();
Model.findOneAndUpdate(
{ _id: mongoose.Types.ObjectId() },
{},
{
'new': true,
upsert: true,
setDefaultsOnInsert: true
},
function(err, model) {
// model has createdAt and updatedAt
});
Model.update(
{ _id: mongoose.Types.ObjectId() },
{ counter: 0 },
{ upsert: true },
function(err, model) {
// model has createdAt and updatedAt
});Rename the properties using options.
minute(schema, {
createdAt: 'created_at',
updatedAt: 'updated_at'
});1.0.0
10 years ago