Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Mongoose support the Mongodb `findAndModify` method?

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!");      } }); 
like image 647
Raja Avatar asked Sep 07 '11 13:09

Raja


People also ask

How do I use findAndModify in MongoDB?

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.

How MongoDB works with Mongoose?

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.

Can you use Mongoose and MongoDB together?

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.

What is Mongoose model method?

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.


2 Answers

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); 
like image 199
furf Avatar answered Oct 10 '22 18:10

furf


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

like image 41
juanpaco Avatar answered Oct 10 '22 17:10

juanpaco