我想更新一个数组并返回文档。我的 findAndModify 语法正确吗?
this.becomeFollower = function(title, username, callback){
"use strict"
posts.findAndModify({
query: {"title":title, "roster":"yes"},
update: { "$addToSet": { "followers":username } },
new: true,
upsert: true
},
function(err, doc){
console.log('find and modified ' +doc);
});
}
我使用这个没有问题:
posts.update({"title":title, "roster":"yes"}, { "$addToSet": { "followers":username } }, function(err, roster){
"use strict"
if(err) return callback(err, null);
callback(err, roster);
});
查看node-mongodb findAndModify 的文档 https://github.com/mongodb/node-mongodb-native#find-and-modify;签名看起来像:
collection.findAndModify(query, sort, update, options, callback)
所以你应该这样做:
posts.findAndModify(
{"title":title, "roster":"yes"},
[['_id','asc']],
{ "$addToSet": { "followers":username } },
{new: true, upsert: true},
function(err, doc){
console.log('find and modified ' +doc);
}
);
The sort
参数可能是可选的,但尚不清楚,因此我将其包含在示例中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)