很抱歉问了这个非常基本的问题,但我真的希望这段代码能够工作。
在node.js中,我使用.updateOne()
函数成功更新了我的mongo数据库,并将参数保留为空白,如下所示。我认为它会更新集合中最近添加的文档,但它更改了最旧的文档。
如何更新集合中最近添加的文档而不循环遍历整个集合?任何建议都有帮助,我愿意使用其他功能,但不想这样做。
“重要部分”:
var collection = db.collection('SensorValues');
collection.updateOne({}, { $set: { "VSWR": 999 } }, function (err, result) {
console.log("Look I updated something");
});
最佳答案
只需使用 db.collection.findOneAndUpdate()
而不是 db.collection.updateOne()
添加第三个参数选项 returnNewDocument: true
。
returnNewDocument When true, returns the updated document instead of the original document. Defaults to false.
查看更多信息 https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
关于javascript - MongoDB updateOne 获取最旧的文档 我想要最新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838622/