【问题标题】:How to get count on MongoDB's Motor driver?如何依靠 MongoDB 的电机驱动程序?
【发布时间】:2020-03-29 19:38:52
【问题描述】:

我想统计一下 Motor's Diver,但我收到了这个错误。

AttributeError: 'AsyncIOMotorCursor' object has no attribute 'count'

这是我的代码:

await MOTOR_CURSOR.users.find().count()

【问题讨论】:

标签: python mongodb pymongo motorengine


【解决方案1】:

MotorCollection.find() 返回AsyncIOMotorCursor 并且它没有count 方法。相反,您应该致电 MotorCollection.count_documents()

await db.users.count_documents({'x': 1})

另外值得注意的是,您所指的MOTOR_CURSORMotorDatabase 实例,最好将其称为db 实例而不是游标以避免混淆。

【讨论】:

  • 速度太慢了。与普通 mongo 的 count() 不同,后者立即返回结果。但后者似乎无法通过 Motor 获得:|
  • @PavelVlasov,count() 函数是一个遗留函数,已在所有 MongoDB 客户端接口(即mongo)中弃用。它已被count_document()estimated_document_count() 取代。旧版count() 本质上是estimated_document_count()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 2013-02-08
  • 2016-11-09
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
相关资源
最近更新 更多