MongoDB 将限制设置为 $inc [英] MongoDB set limit to $inc

查看:48
本文介绍了MongoDB 将限制设置为 $inc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有体育运动员的数据库,其中包含他们的技能".例如,数据库中的足球运动员有 14 项技能.每天我都会模拟他们的训练,这样训练有素的技能会增加一些.我的收藏有超过 150 万条记录,所以我会批量更新.问题是使用 $inc,它可以设置值超过限制,在我的情况下,技能限制是 100.我如何设置字段限制,以便在超过限制时不会更新更多?

I have database of sports players which contains their "skills". For example football player in database has 14 skills. Each day I simulate training of them so trained skill that day increases by some amount. My collection has over 1.5 million records, so I do bulk update. The problem is that using $inc, it can set value over limit, in my case the skill limit is 100. How could I set limit to field, so it won't be updated more if it has exceeded its limit?

我也尝试在更新后进行技能修复",我的意思是在训练模拟完成后,我运行脚本来迭代所有玩家,如果它有一个超出值的字段,我会进行更新查询来修复它,但是进行 150 万次单独的查询对服务器来说是致命的,那么还有其他方法吗?

I have also tried to do skills "fixing" after updating, I mean after training simulation is done, I run script which iterates all players and if it has a field of exceeded value, I make update query to fix it, but making 1.5 million separate queries is kill for the server, so are there any other ways of doing it?

增加的值在计算中有一些随机值,所以不是所有玩家都一样.但是该值可以是从 0 到 1,四舍五入到第二位,例如它可以是 0.1, 0.2, 0.3.... 1 .因此,我将具有此值和训练技能的玩家分组,并使用运算符 $in 更新数据库,其中包含训练相同技能且训练值相同的玩家 ID

increased value has some random values in calculations, so it is not the same for all players. But the value can be from 0 to 1, rounded to 2nd digit,for example it can be 0.1, 0.2, 0.3.... 1 . So I group players with this value and trained skill and make update to database using operator $in, which contains players ids who train the same skill and their training value is the same

推荐答案

可以通过查询文档来限制:

You can restrict it by a query document:

db.players.update({skills : {$lt : 100}}, {$inc : {skills : 1}})

这篇关于MongoDB 将限制设置为 $inc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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