尝试在findOneAndUpdate upsert后捕获_id

我第一次使用“upsert”使用findOneAndUpdate。 我正在replace最初的一些难看的代码。

我真的很喜欢重构是多么干净,但我的一个要求是如果创build一个新的logging,我需要捕获_id用于紧接着的另一个进程中。

问题是,如果一个logging不存在,它做了一个“upsert”返回为空。

我试过returnNewDocument:真假,但没有运气。

这里是我的代码结构:

Model.findOneAndUpdate( { "user_email" : req.body.userEmail}, { $set: { "property_1" : req.body.prop1, "property_2": req.body.prop2 } }, { upsert: true, returnNewDocument: true }, function(err, data){ if(err) console.log('Error in find one and update'); console.log('Show data result: ', data); }) 

我认为改变returnNewDocument为“假”会做的伎俩,因为它应该返回原始文件,但是这是行不通的。

有任何想法吗?

谢谢,所有!

你正在使用mongoose 。 它承认new选项,而不是returnNewDocumentreturnNewDocument