node.js - 在 Mongo 中,增量和返回值——可能在 1 个调用中?

标签 node.js mongodb

在Mongo中,是否可以自增并得到自增的结果?

collection.update({id: doc_id}, {$inc: {view_count: 1}});

我试图输出该语句的结果(在 Node 中),我得到了以下内容:

 { _id: 1,
  _state: undefined,
  _result: undefined,
  _subscribers: [] }

最佳答案

您可以使用 findAndModify .添加 new:true 选项

根据文档:

The findAndModify command modifies and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.

您可以执行以下操作:

db.collection.findAndModify(
    query:  {_id: doc_id},
    update: { $inc: { view_count :1 } },
    new: true,
)

关于node.js - 在 Mongo 中,增量和返回值——可能在 1 个调用中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440338/

相关文章:

node.js - MongoDB NodeJS native 驱动程序(mongodb) 与 Mongo Shell 性能对比

javascript - 使用 Mongolab 和 Github 的安全方式。

mongodb - 选择子文档中数组的最后一个值

mongodb - Mongo 查找查询不适用于某些值。

javascript - 在 Hapi.js 中提供静态 JavaScript 文件不起作用

JavaScript:res.sendFile 不是函数

使用源代码行号调试 node.js

node.js - 如何修复导出 'NotificationKind' 中未找到 'rxjs' 错误

python - 如何将上传的文件存储在 Flask 的 mongoDB 中?

android - 从android与socket.io通信