Python 用什么代替游标计数?

Python 用什么代替游标计数?,python,mongodb,collections,Python,Mongodb,Collections,我使用find从数据库中提取数据,并使用count检查是否存在任何内容(或是否为空)。 这是在对我大喊大叫,count被弃用了。改为使用Collection.count\u文档。但当我更改它时,它会说 “Cursor”对象没有属性“count\u documents”。我尝试将集合添加到db.find()中,但仍然一无所获 def stuff(x): return db['stuffs'].find({"stuff": x}) def check_for_stuff(x): t

我使用
find
从数据库中提取数据,并使用
count
检查是否存在任何内容(或是否为空)。 这是在对我大喊大叫,
count被弃用了。改为使用Collection.count\u文档。
但当我更改它时,它会说
“Cursor”对象没有属性“count\u documents”
。我尝试将
集合
添加到
db.find()
中,但仍然一无所获

def stuff(x):
    return db['stuffs'].find({"stuff": x})

def check_for_stuff(x):
    things = stuff(x)
    if not things.count():
        return None

count\u文档
在收集级别工作。因此,使用它的适当方法是使用
DB.collection.count\u documents({“stuff”:x})
查询数据库,方法是将过滤器直接传递给
count\u documents
方法

因此,
check\u for\u stuff
可以是这样的:

def check_for_stuff(x):
    count = db['stuffs'].estimated_document_count({"stuff": x}) //or db['stuffs'].count_documents({"stuff": x}) or db.collection.count_documents({"stuff": x}) depending on your use case
    ......Do what you need to do....
用于提高性能


这就是为什么您需要进行两次数据库调用的原因

发布您正在尝试的操作。发布您迄今为止使用的代码,但我只想调用数据库一次>>,以查找内容。。。你是说我必须再次调用它来分别计算相同的内容?从我在其他地方读到的内容来看也是这样(检查编辑)