Database

[{"collection":"companies","count":4,"content":{"name":{"type":"enum","values":["Google","Microsoft","Apple","Meta"]},"address":{"type":"enum","values":["...","...","...","..."]},"employees":{"type":"enum","values":[38571,73859,55177,88901]},"phone":{"type":"enum","values":[12345,54321,88888,77777]}},"indexes":[{"name":"unique","key":{"name":1},"unique":true}]},{"collection":"toBeInserted","count":4,"content":{"name":{"type":"enum","values":["Apple","Dell","Google","IBM"]},"address":{"type":"enum","values":["...","...","...","..."]},"employees":{"type":"enum","values":[55177,77889,33333,77777]},"phone":{"type":"enum","values":[22222,11223,44444,88888]}}}]

Query

db.toBeInserted.aggregate([{"$project":{/** select the relevant fields*/_id:0,name:1,address:1,employees:1,phone:1}},{"$merge":{"into":"companies","on":"name","whenMatched":"merge","whenNotMatched":"insert"}}])

Result