I have a collection whose data I have to transfer to a new collection. The new collection has some extra fields and I also want to discard some of the fields from the old collection.
Example:
oldCollection { k1:v1, k2:v2, ;k3:v3}
newCollection {k1:v1, k3:v3, k4:v4, k5, v5}
I have to migrate some values and also add new values to the same document while inserting the old data.
You can achieve it using an aggregation pipeline something similar to this:
You can use
$outin case your MongoDB version is less than4.2, in place of$merge. Like this: