Earlier I used to use count() when I used to have a simple function of finding any product for example :
grocery = grocery.find({'Product':{'$regex':'.*?'+resultx,'$options':"i"}})
and then use the count() function on it to get the count like so which worked perfectly fine in giving me the count :
grocery.count() == 0:
But now I am using aggregate like so :
pipeline = [{'$match': {'Product':{'$regex':'.*?'+resultx,'$options':"i"}}},
{'$lookup':{'from': 'othervendors','localField': 'Product','foreignField': 'Product','as': 'Matches'}}]
grocery = db.products.aggregate(pipeline)
and if I use count() on it :
grocery.count() == 0:
It throws me an error :
**AttributeError: 'CommandCursor' object has no attribute 'count'**
My question is, How do I use count() on the aggregate function
aggregate()returns aCommandCursorand not aCursorin the pymongo API. The difference is common to all language API's, and it does not have acount()method. Use a separate pipeline statement with$countor it's$sum: 1equivalent.find()( Thus only the$matchsince$lookupis just adding an array to the matched documents ) then run thecount()from the same query condition on.find(). It's a lot faster than$groupon anull|""value simply to count results.