我檢查了修士的源代碼,並最終使其工作。即使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文件中。