在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/