用表达式而不是值更新 [英] Update with expression instead of value

查看:72
本文介绍了用表达式而不是值更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MongoDB完全陌生...我错过了一个新手"标签,因此专家们不必看到这个问题.

I am totally new to MongoDB... I am missing a "newbie" tag, so the experts would not have to see this question.

我正在尝试使用表达式更新集合中的所有文档.我期望解决的查询是:

I am trying to update all documents in a collection using an expression. The query I was expecting to solve this was:

db.QUESTIONS.update({}, { $set: { i_pp : i_up * 100 - i_down * 20 } }, false, true);

但是,这会导致以下错误消息:

That, however, results in the following error message:

ReferenceError:未定义i_up(shell):1

ReferenceError: i_up is not defined (shell):1

与此同时,数据库在吃这个方面没有任何问题:

At the same time, the database did not have any problem with eating this one:

db.QUESTIONS.update({}, { $set: { i_pp : 0 } }, false, true);

我是否必须一次完成一份文档?这似乎太复杂了.

Do I have to do this one document at a time or something? That just seems excessively complicated.

更新 谢谢塞尔吉奥·图伦采夫(Sergio Tulentsev)告诉我它不起作用.现在,我真的在为如何做到这一点而苦苦挣扎.我向乐于助人的灵魂提供500个利润点,他们可以用MongoDB理解的方式编写此信息.如果您在我们的论坛上注册,我可以在此处将利润点添加到您的帐户中.

Update Thank you Sergio Tulentsev for telling me that it does not work. Now, I am really struggling with how to do this. I offer 500 Profit Points to the helpful soul, who can write this in a way that MongoDB understands. If you register on our forum I can add the Profit Points to your account there.

推荐答案

您不能在更新中使用表达式.或者,相反,您不能使用依赖于文档字段的表达式.简单的自包含数学表达式就可以了(例如2 * 2).

You can't use expressions in updates. Or, rather, you can't use expressions that depend on fields of the document. Simple self-containing math expressions are fine (e.g. 2 * 2).

如果要为所有文档设置一个新字段,这是其他字段的功能,则必须遍历它们并手动更新.多次更新在这里无济于事.

If you want to set a new field for all documents that is a function of other fields, you have to loop over them and update manually. Multi-update won't help here.

这篇关于用表达式而不是值更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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