Last 20260116.md journal, I mentioned that $unionWith may result in duplicate documents. In the example pipeline below, it produced duplicate documents (data are mock/generated). This is because we’re basically just combining the results of the two pipelines. Note that you need to use $sort with the identifier so that you can see them adjacent. Without sorting, the documents from another pipeline may be far apart from the first.
[
{
$match: {
gender: { $regex: ".*ale" }
}
},
{
$unionWith: {
coll: "persons",
pipeline: [
{
$match: {
gender: { $regex: ".*le" }
}
}
]
}
},
{ $sort: { _id: 1 } }
]
Sample result
[
{
"_id": {
"$oid": "696e23bbbea06b125b46d15e"
},
"id": 2,
"first_name": "Ashlee",
"last_name": "Alchin",
"email": "aalchin1@fema.gov",
"gender": "Female",
"ip_address": "157.142.117.33"
},
{
"_id": {
"$oid": "696e23bbbea06b125b46d15e"
},
"id": 2,
"first_name": "Ashlee",
"last_name": "Alchin",
"email": "aalchin1@fema.gov",
"gender": "Female",
"ip_address": "157.142.117.33"
}
]