将集合中的值减1直到0 [英] decrement value in collection until 0

查看:69
本文介绍了将集合中的值减1直到0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用meteorJS,并且有一个user collection,我在其中将一个名为分数"的值存储在用户个人资料中.

I´m using meteorJS and have a user collection where I´ve stored a value called 'score' in the users profile.

现在,我想为每个用户以10的分数值递减来更新集合,但是我在获取每个用户的分数值并像"current value - 10"一样更新它们时遇到了问题.还应该只更新不会小于0的值.

Now, I want to update the collection with a decrement of the score value by 10 for each user but I have problems with getting the score value for each user and update them like "current value - 10". It also should only update the values that will not become lower than 0.

有人可以给我提示如何为每个用户查找和更新配置文件的值吗?

Can someone give me a hint how to find and update the value for each user the profile?

推荐答案

Meteor.users.update({'profile.score': {$gte: 10}}, {$inc: {'profile.score': -10}}, {multi: true});

这能满足您的需求吗?根据需要更改选择器.

Does this accomplish what you need? Change selector as needed.

说明:我们会过滤出得分为10或更高的用户.我们将所有匹配用户的得分增加" -10(因此将其降低10).

Explanation: We filter out users who have a score of 10 or more. We "increase" all of the matching users' scores by -10 (so we decrease them by 10).

这篇关于将集合中的值减1直到0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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