How to rename a field name inside array

Hello @Alikhan_Oitan, Welcome to MongoDB Developer Forum :slight_smile: ,

You can use update with aggregation pipeline starting from MongoDB 4.2,

  • check query update only where groupId is exists
  • $map to iterate loop of lessons and result your required fields
db.collection.update(
  { "lessons.groupId": { $exists: true } },
  [{
    $set: {
      lessons: {
        $map: {
          input: "$lessons",
          in: {
            classid: "$$this.groupId",
            someField: "$$this.someField"
          }
        }
      }
    }
  }],
  { multi: true }
)
4 Likes