表单: PythonNotes
用户: dreamable
创建日期: 2021-03-16 01:10:44 UTC
更新日期: 2021-03-16 01:11:23 UTC
引用:(Table ID 14, Record ID 2)

标题 :
Python3 + MongoDB bug
笔记 :

遇到一个奇怪的问题,在Python3中,调用MongoDB。Sorting会影响结果。

基本代码

   order = pymongo.DESCENDING if self.descending else pymongo.ASCENDING
   items = self.tb.find(cond).sort(self.uid,order).limit(self.limit)
   logging.info(f"There are {items.count()} items")
   for idx,t in enumerate(items):
      logging.info(f"Working on {idx}-th item: {t[self.uid]}")

问题:

  1. 当order为descending,limit> # of records, hang after 100 records
  2. 当order为ascending的时候,问题不存在
  3. 当limit <= # of records时候,问题不存在
  4. 只对一个collection有此问题,其它类似collection没有此问题
  5. 存在{uid: -1}的index,添加{uid:1}的index无帮助

找不到原因,可能是某个地方的bug。采用ascending order绕开了这个问题。

标签: