如何从findOneAndUpdate方法获取更新的文档
How to get updated document back from the findOneAndUpdate method?
我正在使用MongoDB和node js,我使用了npm install mongodb
我想更新现有文档并返回更新的文档,文档已正确更新。 但它返回的旧文档意味着更新前的原始文档。 我使用了returnNewDocument:true
参数但没有使用.
var filter = {
'_id': object_id
},
update = {
$set: { "status" : data["status"] },
$push: {
"statusHistory": {
$each: [{ status:data["status"],statusChangedTime:data["statusChangedTime"],comment:data["comment"]}],
$position:0,
}
},
}
,options = {
//upsert: false,
//multi: false,
returnNewDocument: true
};
col.findOneAndUpdate(filter, update, options,function(err, res) {
if (err) {
console.log(err);
}else {
console.log(res);
}
});
响应是
{ lastErrorObject: { updatedExisting: true, n: 1 },
value:
{
//original document
},
ok: 1 }
当我通过终端直接访问mongoDB并尝试
db.MyCollection.find().pretty();
文档已正确更新,它仅返回原始文档而不是更新的文档。
被困在这里2个小时,任何帮助都值得赞赏
在包中.json
"mongodb": "^2.1.4",
Node.js 驱动程序文档没有提到 findOneAndUpdate()
的returnNewDocument
选项(这是具有相同名称的 MongoDB shell 命令的选项)。
相反,它提到了一个名为 returnOriginal
,默认为 true
。尝试使用该选项,将其设置为 false
以返回更新的文档而不是原始文档。
要在执行更新操作后获取更新的文档,我们需要使用选项 "returnDocument" : "after"
以及 "returnOriginal" : false
.
根据最新的 mongodb 节点驱动程序 v3.6 文档,不推荐使用 returnOriginal。但是当我尝试只包含"returnDocument" : 'after'
而不"returnOriginal":false
它返回原始记录而不是更新的记录时。同时使用它们将提供更新记录的所需输出,而不是原始记录。(来源:http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#findOneAndUpdate)
选项 "returnNewDocument" : true
是一个 mongo shell 选项,根据官方 mongodb 文档(此处提到 https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/),可能无法在节点驱动程序中工作
如果你看到的是最新的2018年,则返回NewDocument:true和returnOriginal:false都不起作用。您必须将名为 new 的属性设置为 true,如查询的选项对象中的 {.., new: true} 所示。
=== 2021 年 8 月
随着 node.js 客户端 v4 的发布,似乎 returnOriginal: false
的旧解决方案(无论如何都很糟糕)不再是正确答案。
若要查看节点的可用选项列表.js请查找 OneAndUpdate 方法:https://mongodb.github.io/node-mongodb-native/4.0/interfaces/findoneandupdateoptions.html
但简而言之,这应该有效:
const doc = await <Collection>.findOneAndUpdate(
{ ... search },
{
$set: {
field1: 'value 1',
field2: ['value 2'],
etc.
},
},
{
upsert: true,
returnDocument: 'after', // this is new !
}
)
与命令findOneAndUpdate()
的本机MongoDB shell相比,NodeJS MongoDB驱动程序具有不同的参数。如果您使用的是"mongodb":"^2.1.4",请使用
返回原件:假
相反
返回新文档:真
.
让我们看看下面的代码:
db.collection('user_setting').findOneAndUpdate({user_id: data.user_id}, {$set: data}, {projection: dbConfig.userSetting, returnOriginal: false}, function (err, res) {
if (err) {
callback({'error': 1, 'message': 'Internal server error! ' + err, 'data': null, 'status': 500});
} else {
console.log(res);
/* { lastErrorObject: { updatedExisting: true, n: 1 },
value:
{ user_id: 1,
notification_alert: 1,
notification_sound: 1,
user_setting_id: 2
},
ok: 1
} */
}
});
如果有其他人在猫鼬设置中使用{ returnOriginal: false }
时遇到此问题:
猫鼬使用{ new: true }
而不是{ returnOriginal: false }
。
此处引用了该功能:Mongoose:findOneAndUpdate 不会返回更新的文档,并且在 Mongoose 文档中:
选项:
新建: 布尔值 - 如果为 true,则返回修改后的文档而不是原始文档。 默认为 false(在 4.0 中更改)
因此,使用findOneAndUpdate
将new: true
添加到方法选项时:
...
const options = {
new: true
};
col.findOneAndUpdate(filter, update, options, function (err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
根据 4.0+ 的实际源代码和类型定义,新属性现在new
。将其设置为true
以返回修改后的结果。
interface QueryFindOneAndUpdateOptions extends QueryFindOneAndRemoveOptions {
/** if true, return the modified document rather than the original. defaults to false (changed in 4.0) */
new?: boolean;
...
我正在使用猫鼬:5.8.9
下面有这些行
const option = { new: true }
const account = await Account.findOneAndUpdate(filter, update, option)
这是有效的
我有用。
req.db.collection(collectionName).findOneAndUpdate(
{ _id : commentById._id }, // matching parameters
updateOpts, //updating parameters
{returnOriginal: false} //don't use **returnNewDocument**
);
- 流星收集更新不是在更新文档
- MongoDB findAndModify:>>>查找和更新文档数组中的对象
- Javascript 不会在浏览器中更新文档
- 如何在猫鼬中更新文档后添加一些操作
- Mongoose创建一个文档,如果找不到指定的字段,则更新文档中的数组
- Meteor Update集合-未捕获错误:不允许.不受信任的代码只能通过ID更新文档.[403]
- 基于多个下拉列表值更新文档
- 更新文档,但出现错误:对于未定义的值,强制转换为字符串失败
- 如何在 Meteor 中使用多个输入字段和单个更新按钮更新文档
- 弹性搜索更新文档类型
- Meteor:更新文档数组中的嵌套对象
- 在遍历光标和更新文档时,连接因应用程序错误而关闭
- 如何使用Monk在Javascript中的forEach循环中更新文档
- mongoDB-使用$set和一个对象更新文档
- mongodb在给定的时间间隔更新文档
- 使用mongoDB和node.js更新文档时出错
- 更新文档上的Ajax.准备功能
- CouchDB更新文档
- 创建或更新文档时的触发代码
- 仅当文档与数据库不同时更新文档