gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:26:29 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com