您正在寻找
newoption参数。该
new选项返回新创建的文档(如果创建了新文档)。像这样使用它:
var query = {}, update = { expire: new Date() }, options = { upsert: true, new: true, setDefaultsOnInsert: true };// Find the documentModel.findoneAndUpdate(query, update, options, function(error, result) { if (error) return; // do something with the document});由于
upsert创建文档(如果找不到文档),因此您无需手动创建另一个文档。



