node.js 在 Mongodb 中更新和返回文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24747189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update And Return Document In Mongodb
提问by user3803096
I want to get updated documents. This is my original code and it succesfully updates but doesnt return the document.
我想获得更新的文件。这是我的原始代码,它成功更新但不返回文档。
collection.update({ "code": req.body.code },{$set: req.body.updatedFields}, function(err, results) {
res.send({error: err, affected: results});
db.close();
});
I used toArray function, but this gave error "Cannot use a writeConcern without a provided callback":
我使用了 toArray 函数,但这给出了错误“不能在没有提供回调的情况下使用 writeConcern”:
collection.update({ "code": req.body.code },{$set: req.body.updatedFields}).toArray( function(err, results) {
res.send({error: err, affected: results});
db.close();
});
Any ideas?
有任何想法吗?
回答by Jonathan Lonowski
collection.update()will only report the number of documents that were affected to its own callback.
collection.update()只会将受影响的文档数量报告给它自己的回调。
To retrieve the documents while modifying, you can use collection.findOneAndUpdate()instead (formerly .findAndModify()).
要在修改时检索文档,您可以collection.findOneAndUpdate()改用(以前是.findAndModify())。
collection.findOneAndUpdate(
{ "code": req.body.code },
{ $set: req.body.updatedFields },
{ returnOriginal: false },
function (err, documents) {
res.send({ error: err, affected: documents });
db.close();
}
);
Update:When executing .findOneAndUpdatein mongoDB then use {returnNewDocument: true}or if using mongoose you can use {new : true}. Above is with Node.js driver.
更新:.findOneAndUpdate在 mongoDB 中执行时然后使用{returnNewDocument: true}或者如果使用mongoose你可以使用{new : true}. 上面是 Node.js 驱动程序。
Note: Currently refers to the Node.js Driver as of version 2.2. For future versions, check for deprecation warnings within the documentation and use the recommended alternatives instead.
注意:当前是指版本 2.2 中的 Node.js 驱动程序。对于未来版本,请检查文档中的弃用警告并改用推荐的替代方案。
回答by milosnkb
The solution is to set: {returnOriginal: false}.
解决方法是设置:{returnOriginal: false}。
collection.findOneAndUpdate(
whereObj,
updateObj,
{returnOriginal: false});
回答by mido
Could not find any way to update many and return the modified records in docs, so I made a workaround.
找不到任何方法来更新许多文件并在docs 中返回修改后的记录,所以我做了一个解决方法。
At least one fault that I can find with below method is, you would not be able to tell if document is modified or already had the value that you are using:
我可以使用以下方法找到的至少一个错误是,您将无法判断文档是否已修改或已经具有您正在使用的值:
function findAndUpdateMany(filter, updateOptions) {
return collection.find(filter).project({_id: 1}).toArray()
.then(function(matchingIds) {
filter = {_id: {$in: matchingIds}}
return collection.updateMany(filter, updateOptions)
}).then(function() {
return collection.find(filter).toArray()
})
}
回答by Venkat Rangan
Checkout the WriteResult object:
签出 WriteResult 对象:
http://docs.mongodb.org/manual/reference/method/db.collection.update/#writeresults-update
http://docs.mongodb.org/manual/reference/method/db.collection.update/#writeresults-update
WriteResult result = collection.update({ "code": req.body.code },{$set: req.body.updatedFields}, function(err, results) {
res.send({error: err, affected: results});
db.close();
});
result should have something like:
结果应该是这样的:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
If you want the updated results, do another query with the primary key.
如果您想要更新的结果,请使用主键执行另一个查询。

