ItGo.me Focus on IT Recommend

Home > javascript - how to get the return value of findAndModify func in MongoDB with mongoose?

javascript - how to get the return value of findAndModify func in MongoDB with mongoose?

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

I'm using Node.js+mongoose+MongoDB like this:

SomeModelSchema.statics.findAndModify = function (query, sort, doc, options, callback) {
    return this.collection.findAndModify(query, sort, doc, options, callback);
};

SomeModel.findAndModify({}, [], { $inc: { amount: 1 } }, {}, function (err) {
    if (err) throw err;
});

I can increase the amount successfully but I want to get the new value of amount without doing the query again. Is there any way to do that?

javascript node.js mongodb mongoose
|
  this question
edited Jan 21 '14 at 9:24 asked Mar 19 '12 at 16:11 Nano WANG 792 2 11 26

 | 

2 Answers
2

---Accepted---Accepted---Accepted---

Specify {new:true} in the options object. This will cause the result value of findAndModify to be the document after the update is applied, which you can then read from to get the new value of amount.

Details here, in the table: http://www.mongodb.org/display/DOCS/findAndModify+Command

I believe your callback will need to take two arguments, like: function(err, result){

When the callback is fired, result should contain the new document (if err is null).


|
  this answer
answered Mar 19 '12 at 19:57 mpobrien 4,104 23 27

 | 

I checked monk's source code and finally made this working. Even code documentation says how it should be, but it is not visible from documentation on monk's web.

Recommend:javascript - How to return from find in nodejs,mongoose mongodb collection

else { return out; } });} When i uncomment the console.log file in the Model.js , it's log the data matched in the find query, The callback in test.js file doesn't executed, did i successfully

/**
 * findAndModify
 *
 * @param {Object} search query, or { query, update } object
 * @param {Object} optional, update object
 * @param {Object|String|Array} optional, options or fields
 * @param {Function} callback
 * @return {Promise}
 * @api public
 */

It means you can either specify query and update as separate parameters, plus options as third parameter:

notescollection.findAndModify(
  { "_id": id },
  { "$set": { 
      "title": title,
      "content": content
  }},
  { "new": true, "upsert": true },
  function(err,doc) {
    if (err) throw err;
    console.log( doc );
  }
);

Or you can specify query and update as fields of first parameter, plus options as second parameter:

notescollection.findAndModify(
  {
    "query": { "_id": id },
    "update": { "$set": { 
        "title": title,
        "content": content
    }}
  },
  { "new": true, "upsert": true },
  function(err,doc) {
    if (err) throw err;
    console.log( doc );
  }
);

For more on the sources check findAndModify function in collections.js file.


|
  this answer
answered Mar 23 '15 at 22:56 Roman 57 1 7

 | 

Recommend:node.js - How to get the value of a field from mongoose mongodb query

js file(js.js) var fields = "workaholic alcoholic insomniac"; var valid = require("./js"); Hacker.find({name: "arun"}, fields)//, function(err, hackers) { .then(valid.myfun) .catch(err => console.log(err,fields)); This is

------splitte line----------------------------