Does $unionWith run parallel query on independent collections?

Think that I have 3 collections. I want to run $unionWith to fetch data from those three collections.

            db.getCollection('BlogPostLikers').aggregate([
                {$match:{likerId:ObjectId('5eb17cf53f0000020ac10c75')}},
                {$unionWith: { coll: "PostOrBlogs", pipeline: [{ $match: { postType: 'T' } }] }} ,
                {$unionWith: { coll: "PaySlip", pipeline: [{ $match: { employeeId: ObjectId('5eb17cf53f0000020ac10c75') } }] }} ,

        ])

Will this be parallel or sequential ?

Hi @Md_Mahadi_Hossain,

Each stage in aggregation is sequential to the previous one even $unionWith.

the $facet stage should run multiple pipelines in one stage, however, I am not sure if it happens in a multi or single threaded way.

Best regards,
Pavel

1 Like

If $unionWiths are not parallel then what is the use of it. Because we can send multiple requests using threads to run query on multiple collections by drivers.

Hi @Md_Mahadi_Hossain,

The idea of $unionWith is that you can query different data sets from various collections and include them into one document stream. In later stages after the union you can aggregate or transform this data using the aggregation frameworks.

A good example is shown in our documentation is creating a sales report from quarterly gathered collections:

Best regards,
Pavel