mongoose.findOneAndUpdate returns 空

mongoose.findOneAndUpdate returns null

我正在使用 mongoose.findOneAndUpdate() 方法在集合中插入和更新文档。但是,如果 table 中没有要更新的内容并且创建了新文档,则 findOneAndUpdate() 回调 returns 为空。

我怎么知道 findOneAndUpdate() 的插入部分是否成功?

TimeTable.getTimeTableModelObject().findOneAndUpdate({ageGroup:'2'}, timeTable, {upsert:true}, function(err, foundData){});

查看文档: http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate

Available options

new: bool - if true, return the modified document rather than the original. defaults to false

[...]

此外,您可以在回调函数中检查 err