skip to content
Alvin Lucillo

Union pipelines

/ 1 min read

If you have two aggregation pipelines that you want to merge, you can use $unionWith. These two pipelines act as OR, where results are combined. This means this could result to dupliate results. coll here is the same collection as the first pipeline. The logic in this pipeline can be achieved with $or. But if you have complex requirements that requires you to separate them but run against the same collection, you can do the same logic.

[
	{
		"$match": {
			"filesize": {
				"$eq": 123
			}
		}
	},
	{
		"$unionWith": {
			"coll": "coll1",
			"pipeline": [
				{
					"$match": {
						"filesize": {
							"$eq": 456
						}
					}
				}
			]
		}
	}
]