简体   繁体   English

您如何每次递增一个字段,而仅当它大于某个具有multi true的数时才递增另一个字段?

[英]How do u increment one field every time and the other only if it's greater than a certain number with multi true ?

The following code decrements class_number just fine, but not number_of_classes because of the $gt operator. 下面的代码递减class_number就好了,但不是number_of_classes因以$gt运营商。

posts.update({ 'title':doc.title, 'author':doc.author, 'class_number':{ '$gt': doc.class_number } }, 
             { '$inc': { 'number_of_classes':-1 , 'class_number':-1} }, 
             { multi: true }, function(err, dox){....

I need to decrement number_of_classes once every time, but only each class_number above a certain value. 我需要每次减少number_of_classes一次,但只有每个class_number高于某个值。 Is there a way to do this in one statment? 有没有一种方法可以做到这一点?

No there is no way to "conditionally" update one field and not another, just as there is no way to "reference" an existing value of a field in a document during an update statement. 就像无法在更新语句期间“引用”文档中某个字段的现有值一样,没有办法“有条件地”更新一个字段,而不能“有条件地”更新另一个字段。 Much as your current statement provides, you can only perform the "conditions" on the query that you pass to the update. 就像您当前的语句所提供的一样,您只能对传递给更新的查询执行“条件”。

So if you are trying to hit a "floor" of 0 then your "safe" method is in two operations: 因此,如果您尝试将“底数”设置为0,则“安全”方法有两种操作:

db.collection.update(
     { "title":doc.title, "author":doc.author },
     { "$inc": "number_of_classes": -1 }
)

db.collection.update(
     { 
         "title":doc.title, 
         "author":doc.author,
         "$and": [
             { "class_number": { "$gt": 0 } },
             { "class_number": { "$gt": doc.class_number } }
         ]
     },
     { "$inc": "class_number": -1 }
)

Or however that logic works for you in actual updates. 或者,该逻辑在实际更新中对您有用。

In future versions ( as of writing ) you can "batch" these updates into a single request, but they actually are still two operations, even though sent to the server only once. 在将来的版本中(撰写本文时),您可以将这些更新“批处理”到单个请求中,但是,即使仅发送到服务器一次,它们实际上仍然是两个操作。

I do somehow suspect that due to the way you are writing this statement that you are actually trying to solve some kind of "allocation" problem. 我确实怀疑由于您编写此语句的方式,您实际上正在尝试解决某种“分配”问题。 And that would be a different question to the way you are asking this here. 这与您在此处提出问题的方式将是一个不同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript Increment number 每次(if 语句)为真 - JavaScript Increment number every time (if statement) is true 在for循环中,如何每隔一次递增一次计数器? - In a for loop, how do I increment a counter once every other time? 如何检查一个值是否大于另一个 - How to check if one value is greater than other 如何隐藏div直到某个字段的值大于零 - How to hide a div until a certain field(s) value is greater than nothing 计算多少次 document.hasFocus 返回“true”,如果它等于或大于 15 做某事 - Count how many times document.hasFocus returns “true”, if it's equal or greater than 15 do something 如何用POSTMAN测试某些东西是否大于某个数字 - How to test if something is greater than certain number with POSTMAN 我如何逐行读取并且仅返回数字大于0.0的行 - How do I read line by line and only return lines that have a number greater than 0.0 当字段大于/小于其他字段时,如何使用 Mongoose 查找和更新 - How to Find And Update With Mongoose When a Field is Greater / Lesser Than Other 如何防止在html的数字字段中插入大于最大值的值 - How to prevent inserting value that is greater than to max in number field in html 如何验证该字段大于仅在检查特定按钮时 - How to verify that field is greater than only if check on a specific button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM