timed eval1 DeprecationWarning count is deprecated. Use Collection.count_documents instead

淡淡的烟草味﹌ 2023-01-05 05:18 268阅读 0赞

timed eval :1: DeprecationWarning: count is deprecated. Use Collection.count_documents instead.

使用pymongo来操作mongodb数据库,在统计查询数量的时候出现抛出这样的警告,原因是Collection.count这样的方式将被弃用,用Collection.count_documents来替代。下面是新旧两种统计查询数据的比较。

旧的方式 Collection.count

  1. %%time
  2. collection.find({ 'used': 0}).count()
  3. <timed eval>:1: DeprecationWarning: count is deprecated. Use Collection.count_documents instead.
  4. Wall time: 343 ms
  5. 1000000

或者

  1. %%time
  2. collection.count({ 'used': 0})
  3. <timed eval>:1: DeprecationWarning: count is deprecated. Use estimated_document_count or count_documents instead. Please note that $where must be replaced by $expr, $near must be replaced by $geoWithin with $center, and $nearSphere must be replaced by $geoWithin with $centerSphere
  4. Wall time: 370 ms
  5. 1000000

新的方式 Collection.count_documents

  1. %%time
  2. # collection.count_documents({})
  3. collection.count_documents({ 'used': 0})
  4. Wall time: 460 ms
  5. 1000000

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读