【发布时间】:2015-01-29 02:31:31
【问题描述】:
我正在使用 python 的 pymongo 库向 MongoDB(2.6 版)提交一个非常简单的查询:
query = {"type": "prime"}
logging.info("Querying the DB")
docs = usaspending.get_records_from_db(query)
logging.info("Done querying. Sorting the results")
docs.sort("timestamp", pymongo.ASCENDING)
logging.info("Done sorting the results, getting count")
count = docs.count(True)
logging.info("Done counting: %s records", count)
pprint(docs[0])
raise Exception("End the script right here")
get_records_from_db() 函数非常简单:
def get_records_from_db(query=None):
return db.raws.find(query, batch_size=50)
请注意,我实际上需要处理所有文档,而不仅仅是docs[0]。我只是想以docs[0] 为例。
当我运行这个查询时,我得到的输出是:
2015-01-28 10:11:05,945 Querying the DB
2015-01-28 10:11:05,946 Done querying. Sorting the results
2015-01-28 10:11:05,946 Done sorting the results, getting count
2015-01-28 10:11:06,617 Done counting: 559952 records
但是我再也回不来docs[0]。我在{"timestamp": 1} 和{"type": 1} 上有一个索引,查询似乎工作得相当好(因为返回的计数非常快),但我不确定为什么我永远不会取回实际文档(文档很小[ 50K以下])。
【问题讨论】:
-
是不是因为你有一些超高的日志记录设置?
-
不确定 pymongo 中的日志记录会如何影响性能,但日志记录设置为
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)