如何使用mongoose获取MongoDB中findAndModify func的返回值? [英] how to get the return value of findAndModify func in MongoDB with mongoose?
问题描述
我正在使用Node.js + mongoose + MongoDB,如下所示:
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?
推荐答案
指定 {new:true}
在选项
对象中。这将导致 findAndModify
的结果值成为应用更新后的文档,然后您可以读取该文档以获取金额的新值
。
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
.
此处的详细信息,在表格中: http://www.mongodb.org/display/DOCS/findAndModify+Command
Details here, in the table: http://www.mongodb.org/display/DOCS/findAndModify+Command
我相信你的回调需要拿两个参数,例如:
function(错误,结果){
I believe your callback will need to take two arguments, like:
function(err, result){
当回调被触发时, result
应该包含新文档(如果 err
为null)。
When the callback is fired, result
should contain the new document (if err
is null).
这篇关于如何使用mongoose获取MongoDB中findAndModify func的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!