当我尝试在节点上的mongoDB数据库上运行findandModify()方法时,我会遇到错误。
我遇到的错误是:
[MongoError:例外:必须指定删除或更新]
我觉得很奇怪,因为我指定了"更新",我的代码如下。
var techId = req.params.id,
collection = db.collection('tech');
collection.findAndModify({
query: { _id: techId},
update: { $inc: { score: 1 } }
}, function(err, doc){
console.log(err, doc);
});
您正在使用错误的语法,有太多的对象。
尝试:
db.collection('tech').findAndModify(
{_id: techId}, // query
{$inc: { score: 1 }}, // update
function(err, object) {
console.log(err, doc);
}
);
http://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html