I would like to use findAndModify to atomically increment a field, using Mongoose.
However, the code below throws the error "TypeError: Object # has no method 'findAndModify'":
// defining schema for the "counters" table var tableSchema = new Schema({ _id: String, next: Number }); // creating table object for the counters table var counters_table = mongoose.model('counters', tableSchema); var tableObj = new counters_table(); // increment the "next" field on the table object var query = {_id: 'messagetransaction'}; var update = {'$inc': {next: 1}}; var ret = tableObj.findAndModify(query, [], update, true, true, function(err) { if (err) { throw err; } else { console.log("updated!"); } });
MongoDB – FindAndModify() Method. The findAndModify() method modifies and return a single document that matches the given criteria. By default, this method returns a pre-modification document. To return the document with the modifications made on the update, use the new option and set its value to true.
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.
yes you should, its a good practice. Mongoose requires a connection to a MongoDB database. You can use require() and connect to a locally hosted database with mongoose.
A Mongoose model is the compiled version of the schema definition that maps directly to a single document in a MongoDB collection. An instance of a model is called a document. Mongoose models are responsible for querying, creating, updating, and removing documents from the MongoDB database.
The feature is not well (read: at all) documented, but after reading through the source code, I came up with the following solution.
Create your collection schema.
var Counters = new Schema({ _id: String, next: Number });
Create a static method on the schema which will expose the findAndModify method of the model's collection.
Counters.statics.findAndModify = function (query, sort, doc, options, callback) { return this.collection.findAndModify(query, sort, doc, options, callback); };
Create your model.
var Counter = mongoose.model('counters', Counters);
Find and modify!
Counter.findAndModify({ _id: 'messagetransaction' }, [], { $inc: { next: 1 } }, {}, function (err, counter) { if (err) throw err; console.log('updated, counter is ' + counter.next); });
Bonus
Counters.statics.increment = function (counter, callback) { return this.collection.findAndModify({ _id: counter }, [], { $inc: { next: 1 } }, callback); }; Counter.increment('messagetransaction', callback);
This is fully supported in Mongoose 3.x now, though the name is slightly different.
http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
http://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate
http://mongoosejs.com/docs/api.html#model_Model.findOneAndRemove
http://mongoosejs.com/docs/api.html#model_Model.findByIdAndRemove
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With