With the release of MongoDB 4.4 comes a new aggregation pipeline stage called $unionWith. This stage lets you combine multiple collections into a single result set!

Here’s how you’d use it:

Simplified syntax, with no additional processing on the specified collection

db.collection.aggregate([
  { $unionWith: "<anotherCollection>" }
])

Extended syntax, using optional pipeline field

db.collection.aggregate([
  { $unionWith: { coll: "<anotherCollection>", pipeline: [ <stage1>, ... ] } }
])

Your resulting documents will merge your current collection’s (or pipeline’s) stream of documents with the documents from the collection/pipeline you specify. Keep in mind that this can include duplicates!

#mongodb

How to Use the Union All Aggregation Pipeline Stage in MongoDB 4.4
18.85 GEEK