【发布时间】:2014-07-22 05:05:33
【问题描述】:
我在猫鼬中使用以下更新查询:
Doc.findOneAndUpdate({
name: 'Ekik'
}, {$setOnInsert: {key: value}}, {upsert: true, new: true}, function (err, doc) {
// How to get upserted count here?
console.log(doc.isNew); // always true either document upserted or don't
});
我需要计算插入文档的数量。我该怎么办?
【问题讨论】:
-
您确实意识到您将 ObjectId 值作为查询传递?要么你只是生成了这个,所以 一个插入,或者你从某个地方得到它并期望它存在,所以 一个更新。
-
我需要像下面的stackoverflow.com/questions/16358857/… 一样破解,但我只需要知道任一文档已插入
-
再看评论,很清楚。您正在通过 _id 查找。它是否存在于集合中。方法名称中的“一”应该提示您有多少。
-
不幸的是,我不明白我怎么能得到 upserted 文档。要么我使用 {new: true} 我总是得到一些文件,但我不知道它是被插入还是存在
-
new:
true是默认修改的文档。尝试新的:false
标签: node.js mongodb mongoose mongodb-query