早些时候我习惯使用count(),因为我曾经有一个简单的查找任何产品的功能,例如:
grocery = grocery.find({'Product':{'$regex':'.*?'+resultx,'$options':"i"}})
然后使用count()函数来获取计数,这样可以很好地计算出来:
grocery.count() == 0:
但现在我正在使用聚合:
pipeline = [{'$match': {'Product':{'$regex':'.*?'+resultx,'$options':"i"}}},
{'$lookup':{'from': 'othervendors','localField': 'Product','foreignField': 'Product','as': 'Matches'}}]
grocery = db.products.aggregate(pipeline)
如果我对它使用count():
grocery.count() == 0:
它给我一个错误:
**AttributeError: 'CommandCursor' object has no attribute 'count'**
我的问题是,如何在聚合函数上使用count()