Node.js 如何在节点中的MongoDB更新中返回完整文档
我试图在MongoDB中使用Node.js 如何在节点中的MongoDB更新中返回完整文档,node.js,mongodb,Node.js,Mongodb,我试图在MongoDB中使用{fullResult:true}选项 collection.update(query, payload, { fullResult: true }, function(err, result) { console.log(result); }); 虽然它确实改变了result的值,但我不会取回文档。相反,我回来了: [ { updatedExisting: true, n: 1, lastOp: { _bsontype: 'Timestamp'
{fullResult:true}
选项
collection.update(query, payload, { fullResult: true }, function(err, result) {
console.log(result);
});
虽然它确实改变了result的值,但我不会取回文档。相反,我回来了:
[ { updatedExisting: true,
n: 1,
lastOp: { _bsontype: 'Timestamp', low_: 1, high_: 1403025407 },
connectionId: 510045,
err: null,
ok: 1 } ]
为了回到更新的文档,我还应该做些什么吗?使用:
new
选项将在设置为true
时返回更新的文档。如果设置为false
,它将在更新之前返回旧文档
find和modify
还需要一个排序参数。如果排序不重要,那么在_id上排序就可以了,但是尝试在索引的东西上排序
将
fullResult
与update
一起使用并不能实现您的想法。我同意,文件有点混乱。让Node.js驱动程序源在
无论是否使用fullResult
,都会返回result
对象,而不是文档列表。result
对象是通过db.command
从原始数据库命令返回的对象。在使用fullResult
的情况下,它将返回未修改的原始结果对象,但仍然不是文档。它的工作方式与文档相同---它返回完整的结果文档,即报告更新操作的文档。问题是完整的结果文档不包括您想要的已更新文档。可能的副本
collection.findAndModify(query,[['_id',1]],payload,{new:true},function(err,result) {
if ( err ) {
console.warn(err);
} else {
console.log(result);
}
});
568 if(fullResult) return callback(null, result);
569 // Backward compatibility format
570 var r = backWardsCompatibiltyResults(result, 'update');
571 // Return the results for a whole batch
572 callback(null, r.n, r)