cannot use the part (…) to traverse the element

£可爱£侵袭症+ 提交于 2019-12-17 10:06:03

问题


Running mongod 3.6 and attempting to use this example.

Here is the sample data:

> db.students2.find().pretty()
{
    "_id" : 1,
    "grades" : [
        {
            "grade" : 80,
            "mean" : 75,
            "std" : 8
        },
        {
            "grade" : 85,
            "mean" : 90,
            "std" : 6
        },
        {
            "grade" : 85,
            "mean" : 85,
            "std" : 8
        }
    ]
}
{
    "_id" : 2,
    "grades" : [
        {
            "grade" : 90,
            "mean" : 75,
            "std" : 8
        },
        {
            "grade" : 87,
            "mean" : 90,
            "std" : 5
        },
        {
            "grade" : 85,
            "mean" : 85,
            "std" : 6
        }
    ]
}

I am attempting to use the all positional operator as specified in the example:

> db.students2.update({}, { $inc: { "grades.$[].std" : -2 } }, {multi: true})
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 16837,
        "errmsg" : "cannot use the part (grades of grades.$[].std) to traverse the element ({grades: [ { grade: 80.0, mean: 75.0, std: 8.0 }, { grade: 85.0, mean: 90.0, std: 6.0 }, { grade: 85.0, mean: 85.0, std: 8.0 } ]})"
    }
})

Why is this error message occurring? Am I not following the documentation properly?


回答1:


When switching from lower version to higher version for mongodb you have to set setFeatureCompatibilityVersion for your mongodb which

Enables or disables the features that persist data incompatible with earlier versions of MongoDB. You can only issue the setFeatureCompatibilityVersion against the admin database.

You can simply set this by running this command in mongo shell

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


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

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