使用$ inc来增加Mongoose的文档属性 [英] Using $inc to increment a document property with Mongoose

查看:369
本文介绍了使用$ inc来增加Mongoose的文档属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to increment the views count by 1 each time my document is accessed. So far, my code is:

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

$inc的位置在哪里?

推荐答案

从不使用猫鼬,而是快速查看文档

Never used mongoose but quickly looking over the docs here it seems like this will work for you:

# 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();

干杯,祝你好运!

这篇关于使用$ inc来增加Mongoose的文档属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆