Created
August 13, 2019 08:30
-
-
Save islishude/2e4258e245d418630bc1a901a1a392cf to your computer and use it in GitHub Desktop.
update all matched item in array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.test.remove({}) | |
WriteResult({ "nRemoved" : 3 }) | |
> db.test.insert({data:[1,2,3,1]}) | |
WriteResult({ "nInserted" : 1 }) | |
> db.test.insert({data:[2,3,4,5]}) | |
WriteResult({ "nInserted" : 1 }) | |
> db.test.insert({data:[1,2,3,1]}) | |
WriteResult({ "nInserted" : 1 }) | |
> db.test.insert({data:[1,2,3,1]}) | |
WriteResult({ "nInserted" : 1 }) | |
> db.test.updateMany( | |
{ data: 1 }, | |
{ $set: { "data.$[item]": 100 } }, | |
{ arrayFilters: [{ item: 1 }] } | |
); | |
> { "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 } | |
> db.test.find() | |
{ "_id" : ObjectId("5d52743d38bca1d6731a8ef7"), "data" : [ 100, 2, 3, 100 ] } | |
{ "_id" : ObjectId("5d52744038bca1d6731a8ef8"), "data" : [ 2, 3, 4, 5 ] } | |
{ "_id" : ObjectId("5d52744138bca1d6731a8ef9"), "data" : [ 100, 2, 3, 100 ] } | |
{ "_id" : ObjectId("5d52744438bca1d6731a8efa"), "data" : [ 100, 2, 3, 100 ] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment