mongodb push existing field values to another field in an array of objects

db.collection.update({
  name: "Annie"
},
[
  {
    $set: {
      words: {
        $map: {
          input: "$words",
          as: "m",
          in: {
            word: "$$m.word",
            scoreHistory: {
              $concatArrays: [
                {
                  $ifNull: [
                    "$$m.scoreHistory",
                    []
                  ]
                },
                [
                  "$$m.score"
                ]
              ]
            }
          }
        }
      }
    }
  }
])

mongoplayground

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top