Mongodb 3.6.0-rc3 array filters not working?

2019-01-20 17:31发布

问题:

I'm trying to use array filters in mongodb 3.6.0-rc3, exactly like in doc example but not getting any rows affected and no error.

Example is simplified. I know this can be done with $ as positional operator but I'm planing to use this feature for two level nested arrays.

db.getCollection('books').update({},
    {
        $set: { "authors.$[element].firstName": "Joe" }
    },
    {
        arrayFilters: [ { element: { "_id": ObjectId("some_id") } } ],
        multi: true
    })

Anyone tried this yet?

回答1:

Are you typing this in robomongo? It looks like it! If so it won't work. Read my note on Updating a Nested Array with MongoDB where I say this does not work in an "older shell" or anything based on it ( which robomongo is a shell based build ) because of the way the shell helper methods are currently implemented:

NOTE Somewhat ironically, since this is specified in the "options" argument for .update() and like methods, the syntax is generally compatible with all recent release driver versions.

However this is not true of the mongo shell, since the way the method is implemented there ( "ironically for backward compatibility" ) the arrayFilters argument is not recognized and removed by an internal method that parses the options in order to deliver "backward compatibility" with prior MongoDB server versions and a "legacy" .update() API call syntax.

So if you want to use the command in the mongo shell or other "shell based" products ( notably Robo 3T ) you need a latest version from either the development branch or production release as of 3.6 or greater.

So if you want to "play with" the release candidate, either use the bundled mongo shell with that version or simply run your code through any standard driver.



回答2:

this BUG,just upgrade studio-3t version to 2018.6.1,use $ and arrayFilter OK.I try it in mongo4.0.4 and studio-3t