gpt4 book ai didi

mongodb - 使用 $inc 来增加 Mongoose 的文档属性

转载 作者:IT老高 更新时间:2023-10-28 13:07:22 25 4
gpt4 key购买 nike

我想在每次访问我的文档时将 views 计数加 1。到目前为止,我的代码是:

Document
.find({})
.sort('date', -1)
.limit(limit)
.exec();

$inc 在哪里适合?

最佳答案

从未使用过 mongoose,但很快查看了文档 here看起来这对你有用:

# create query conditions and update variables
var conditions = { },
update = { $inc: { views: 1 }};

# update documents matching condition
Model.update(conditions, update).limit(limit).sort('date', -1).exec();

干杯,祝你好运!

关于mongodb - 使用 $inc 来增加 Mongoose 的文档属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621948/

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