Error validating $unionWith value

Hi,everyone:

my code is here:

use('drug')
db.drug_insert.aggregate(   
    [ {
        $unionWith: {
            coll: { "$count": "total" }            
        }
    }  ]
)

but i get a error as below:
Error validating $unionWith value. err=Error getting coll field in $unionWith err=Expected ‘coll’ to be string, but got primitive.D instead

any help?my mongodb version is 4.4

Hi @hj_zhang,

Welcome to MongoDB community!

Your syntax for the unionWith stage is not complete.

You need to specify a collection name to field coll and your pipeline goes into field pipeline.

For example to add the count of drug_insert to the end of the query:

db.drug_insert.aggregate(   
    [ {
        $unionWith: {
            coll: "drug_insert", pipeline : [{ "$count": "total" }            
        }]
    }  ]
)

https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/

But to be honest you can just use the $count stage directly to just get the count.

Best
Pavel