【问题标题】:Pymongo Cursor Iteration BottleneckPymongo 游标迭代瓶颈
【发布时间】:2021-11-12 19:19:42
【问题描述】:

我正在尝试将从“最近”查询返回的这个 MongoDB Cursor() 对象转换为列表数据类型。这似乎是我的代码中的瓶颈。我希望这个操作能在几毫秒内完成。任何帮助将不胜感激。谢谢。

nearest = self.database_objs[common_models.ObjModel().current_geographic_collection].find({"location": {
            "$geoWithin": {
                "$centerSphere": [start, self.distance_radians(self.feet_meter(radius))]}}})

print(list(nearest)) # problem here

【问题讨论】:

    标签: python mongodb list cursor pymongo


    【解决方案1】:

    请注意,find 只是将光标设置到满足查询的第一个点(它搜索集合并在第一个满足条件的文档上停止)。
    list(nearest) 评估结果并加载 所有文件从 mongodb 光标到你的 RAM。如果您的结果查询中有很多文档,则加载它们需要一些时间。
    你可以limit你的结果,它会更快。

    【讨论】:

    • 感谢您的回复!有没有办法返回 list() 数据类型而不是 Cursor()?
    • 不,你总是得到一个光标。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2020-02-04
    • 2012-05-12
    • 1970-01-01
    • 2019-07-15
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多