我检查了修士的源代码,并最终使其工作。即使code documentation说应该如何,但从僧侣的网上documentation不可见。
/**
* 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
*/
这意味着你可以指定查询和更新作为单独的参数,再加上选项作为第三个参数:
notescollection.findAndModify(
{ "_id": id },
{ "$set": {
"title": title,
"content": content
}},
{ "new": true, "upsert": true },
function(err,doc) {
if (err) throw err;
console.log(doc);
}
);
也可以指定查询和更新作为第一参数的字段,加选项作为第二参数:
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);
}
);
关于源代码检查的更多信息findAndModify函数在collections.js文件中。