|
The section for $merge stage is missing an example for whenMatched set for an update pipeline to merge subdocument fields if present
It would be nice to have at least a basic example added. Something along these lines:
replset:PRIMARY> db.b.find()
|
{ "_id" : ObjectId("640bc2b614d5a9fb72bca9b4"), "a" : { "b" : 1 } }
|
replset:PRIMARY> db.a.find()
|
{ "_id" : ObjectId("640bc2b614d5a9fb72bca9b4"), "a" : { "a" : 1 } }
|
replset:PRIMARY> db.b.aggregate([{"$merge": { "into": "a", "on": "_id", "whenMatched": [ { "$set": { "a.b": "$$new.a.b" } } ], "whenNotMatched": "discard"}}] )
|
replset:PRIMARY> db.a.find()
|
{ "_id" : ObjectId("640bc2b614d5a9fb72bca9b4"), "a" : { "a" : 1, "b" : 1 } }
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/
|