0

I have an existing mongo collection. Each document inside looks similar to this:

document1:

{
  "_id": ObjectId("..."),
  "subId": 11,
  ...
}

document2:

{
  "_id": ObjectId("..."),
  "subId": 11,
  ...
}

...

documenntN:
{
  "_id": ObjectId("..."),
  "subId": 11,
  ...
}

Is it possible to run a db.collection.update query in such a way as I can replace the "subId": 11 to "subId": 22?

output:

document1:

{
  "_id": ObjectId("..."),
  "subId": 22,
  ...
}

document2:

{
  "_id": ObjectId("..."),
  "subId": 22,
  ...
}

...

documenntN:

{
  "_id": ObjectId("..."),
  "subId": 22,
  ...
}
DCinkaz
  • 13
  • 2
  • Does this answer your question? [MongoDB: How to update multiple documents with a single command?](https://stackoverflow.com/questions/1740023/mongodb-how-to-update-multiple-documents-with-a-single-command) – turivishal Dec 29 '20 at 12:16

1 Answers1

1

db.collection.updateMany({ subId: 11 }, { $set: { subId: 22 } })

Andrey Markeev
  • 1,344
  • 15
  • 20