DCinkaz
DCinkaz

Reputation: 13

mongodb replace string in all documents

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,
  ...
}

Upvotes: 0

Views: 759

Answers (1)

Andrey Markeev
Andrey Markeev

Reputation: 1404

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

Upvotes: 1

Related Questions