【发布时间】:2021-08-29 17:39:41
【问题描述】:
我在 mongodb 中有一个文档。此函数中的文档能够找到该文档并将其返回,但我无法终生使用此函数将计数字段更新为 1。如果有帮助,mongodb 中的当前文档是:
{
_id: 60c5e4c3fba7edd232cf57e7,
counterName: 'give_me_the_count',
count: 1,
updatedAt: 2021-06-13T11:47:02.575Z
}
代码将原始文档返回给 updatedCounter。但 updatedCounter.count 未定义。
async function updateAndReturnNewCount() {
let doc = await Counter.findOne({ counterName : "give_me_the_count" })
var count = doc.count
let updatedCounter = await Counter.findOneAndUpdate({ counterName : "give_me_the_count" },
{ $inc: {"count" : 1 }},
{ new: true,
useFindAndModify: false },
function(err, doc) {
if (err) { console.log(err)}
else {console.log("success")}
}
);
console.log("updateAndReturnNewCount fired")
return updatedCounter.count
}
【问题讨论】:
标签: node.js mongodb express mongoose nosql