Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb 3.6.0-rc3 array filters not working?

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?

like image 465
Senad Mehic Avatar asked Dec 14 '22 19:12

Senad Mehic


1 Answers

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.

like image 121
Neil Lunn Avatar answered Jan 12 '23 16:01

Neil Lunn