Shift all elements of an array field to another array field throughout the whole collection

I solved it using a pipeline inside update command:

db.teamupparticipants.update(
    { },
    [{
        "$set": {
            selectedProfiles: {
                $map: {
                    input: "$appliedProfiles",
                    as: "profile",
                    in: "$$profile"
                }
            },
            appliedProfiles: []
        },
    }],
    { multi: true }
)

Make sure to include the update query inside an array so as to make it a pipeline.

1 Like