Count and filter pipelines using mongodb go driver aggregate

I have run the aggregate command as below:

cur, err := collection.Aggregate(
             bson.M{"$match": generateQueryFilter(query)},
             bson.M{"$set": bson.M{"count": "$count"}},
             bson.M{"$match": generatePaginationFilter(query)},
             bson.M{"$sort": sort},
             bson.M{"$facet": bson.M{
                 "data": []bson.M{bson.M{"$limit": query.Limit}},

but I’m not sure how to get the count that I calculated from

bson.M{"$set": bson.M{"count": "$count"}}, 

I want the count before the 2nd $match… Also, I am not aware of any other way I can get the data field besides using facet. Is there any other way? It looks weird that I use facet with only 1 pipeline. I have tried using $set but it seems $set and $limit can’t be used in one pipeline.

Hi @Ariel_Ariel,

Can you please share what your expected pipeline output is? I think with that, we can come up with a plan on how to get there.


1 Like