Skip to content

Instantly share code, notes, and snippets.

@islishude
Created August 13, 2019 08:30
Show Gist options
  • Save islishude/2e4258e245d418630bc1a901a1a392cf to your computer and use it in GitHub Desktop.
Save islishude/2e4258e245d418630bc1a901a1a392cf to your computer and use it in GitHub Desktop.
update all matched item in array
> 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