Interesting phenomenon of updating two-dimensional array by Mongodb update operation

< H1 > problem description < / H1 >

has the following data:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

this time the elements in this array are deleted correctly.

< H1 > final question < / H1 >

WHY?

is there a potential relationship between the query in the udpate operation and the following modification operation, or is it related to the condition in $pull?

Apr.08,2022

< H1 > answer < / H1 >

as I guessed, it's a matter of detail.

By default, the update () method updates a single document.

by default, update only modifies the first piece of data that matches.

in the statement I use:


rs0:PRIMARY> db.test.insert({ "_id" : 2, "name" : "li", "favorites" : [ "apples", "pudding", "pie", "cake", [ "cake" ] ] })
rs0:PRIMARY> db.test.update({},{$pull:{favorites:{$in:[['cake']]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs0:PRIMARY> db.test.find()
{ "_id" : 2, "name" : "li", "favorites" : [ "apples", "pudding", "pie", "cake" ] }

it seems that I can't reproduce the problem. What's your version?

Menu