MongoDB cannot use the part to traverse element

心不动则不痛 提交于 2019-11-28 11:09:27

问题


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
    ]
}

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.

To not have to retrieve the index of the enabled dashboard, I use the identifier $[] to set all dashboards to false.

This is my request :

db.Project.update({
    _id: my_pro_id
},
{
    $set: {
        "dashboard_group.$[].dgr_enable": false
    }
}, function(err) {

});

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 ?


回答1:


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:

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" } )



回答2:


To everyone in the same case, the solution dnickless gave works for me:

In case you've upgrade from an older version try running this:

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )



来源:https://stackoverflow.com/questions/48949111/mongodb-cannot-use-the-part-to-traverse-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!