MongoDB无法使用零件遍历元素 [英] MongoDB cannot use the part to traverse element
问题描述
我在数据库中有一个像这样的项目模式:
I have in my db a Project's schema like this :
Project: {
_id: ObjectID(),
// some data
dashboard_group: [
0: {
_id: ObjectID(),
dgr_nom: String,
dgr_enable
},
// others dashboards
]
}
对于每个项目,只能启用一个仪表板.因此,在我在"dashboard_group"数组中添加新的仪表板(启用)之前,必须将所有其他仪表板(dgr_enable)设置为false.
For every project, it can have only one dashboard enable. So, before I add a new dashboard (enabled) in the 'dashboard_group' array, I have to set all the others dashboards (dgr_enable) on false.
为了不必检索已启用仪表板的索引,我使用标识符$ []将所有仪表板设置为false.
To not have to retrieve the index of the enabled dashboard, I use the identifier $[] to set all dashboards to false.
这是我的要求:
db.Project.update({
_id: my_pro_id
},
{
$set: {
"dashboard_group.$[].dgr_enable": false
}
}, function(err) {
});
但是Mongo向我返回此错误消息:
But Mongo return me this error message :
"cannot use the part (dashboard_group of dashboard_group.$[].dgr_enable) to traverse the element ({dashboard_group: [ { _id: ObjectId('such id'), dgr_nom: "much noun", dgr_enable: true } ]})"
我已经尝试使用标识符$,它没有返回任何错误,但是字段始终为真.
I already tried with the identifier $, it's not returning any error but the fields is allways true.
任何人都可以理解为什么此请求失败的原因吗?
Can anyone understand why this request fails ?
推荐答案
该行为通常是由更新的MongoDB安装引起的. MongoDB中内置了一个功能兼容级别"开关,该开关允许更新到较新版本,而不会以非预期的方式(不会)改变(某些)旧版本的行为. 文档,用于从v3.4升级到v3.关于该主题的6种状态:
That behaviour is often caused by an updated installation of MongoDB. There is a "feature compatibility level" switch built into MongoDB which allows for updates to a newer version that do not alter (some of) the behaviour of the old version in a non-expected (oh well) way. The documentation for upgrades from v3.4 to v3.6 states on that topic:
对于从3.4升级的部署[默认功能兼容性级别为"3.4",直到将FeatureCompatibilityVersion设置为"3.6"为止.
For deployments upgraded from 3.4 [the default feature compatibility level is] "3.4" until you setFeatureCompatibilityVersion to "3.6".
您可以通过运行以下命令来解决此问题:
You can fix this by running the following command:
db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
这篇关于MongoDB无法使用零件遍历元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!