Using $replaceOne on arrays

  • You can use $mergeObjects operator to merge current object properties with updated property,
  • to prevent null value use $ifNull and $$REMOVE operator
[
  {
    $set: {
      content: {
        $map: {
          input: "$content",
          in: {
            $mergeObjects: [
              "$$this",
              {
                teaser: {
                  $map: {
                    input: "$$this.teaser",
                    in: {
                      $mergeObjects: [
                        "$$this",
                        {
                          uri: {
                            $ifNull: [
                              {
                                $replaceOne: {
                                  input: "$$this.uri",
                                  find: "mydomain.com",
                                  replacement: "anotherdomain.com"
                                }
                              },
                              "$$REMOVE"
                            ]
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
]
1 Like