How to modify values in object list in MongoDB

Viewed 24

How can I remove "(LIVE)" from each value in numbers

{
  "_id": {
    "$oid": "601bc57b1ab1854675870d24"
  },
  "name": "Some name",
  "numbers": [
    "APHYD00(LIVE)",
    "APHYD11(LIVE)"
  ]
}
1 Answers

Try this one:

db.collection.aggregate([
  {
    $set: {
      numbers: {
        $map: {
          input: "$numbers",
          in: {
            $replaceAll: {
              input: "$$this",
              find: "(LIVE)",
              replacement: ""
            }
          }
        }
      }
    }
  }
])

or if you need to update existing values

db.collection.updateMany({ numbers: { $exists: true } }, [
  {
    $set: {
      numbers: {
        $map: {
          input: "$numbers",
          in: {
            $replaceAll: {
              input: "$$this",
              find: "(LIVE)",
              replacement: ""
            }
          }
        }
      }
    }
  }
])

Mongo Playground

Related