Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-15957

[Server] Missing Example for $merge aggregation stage

      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/

            Assignee:
            lauren.tran@mongodb.com Lauren Tran
            Reporter:
            dmitry.ryabtsev@mongodb.com Dmitry Ryabtsev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              26 weeks, 6 days ago