Search code examples
javascriptnode.jsmongodbnode-mongodb-native

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

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 ?


Solution

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