【问题标题】:Sort operation used more than the maximum 33.5 MB of RAM排序操作使用的 RAM 超过了最大 33.5 MB
【发布时间】:2021-12-06 15:44:23
【问题描述】:

在 Pymongo 中,我有以下代码块。我需要将与我的时间搜索查询相关的所有数据都带入内存以进行进一步处理。为此,我使用以下代码块。

search_query = {"time" : {"$lte" : datetime.datetime.now()}}
search_result = mongo_collection.find(search_query)
ascending_search_result = search_result.sort("time", 1)
oldest_time = ascending_search_result[0]["time"]
descending_search_result = search_result.sort("time",-1)
latest_time = descending_search_result[0]["time"] 
list_search_result = list(search_result) 

它:

  • 搜索匹配记录
  • 对它们进行排序以查找开始和结束时间点
  • 使用list(search_result)将其带入内存并转换为列表

但是,当我运行list(search_result) 时,返回以下错误。

 Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.', 'code': 96, 'codeName': 'OperationFailed'}

我可以做些什么来解决这个错误?

编辑我想知道单独抓取搜索结果中的项目是否可以解决问题。不幸的是,这并没有解决问题。抛出同样的错误。我认为这一次只会引入一项,这将是导入记录的最小步骤。

list_search_result = list()
for item in search_result: 
    list_search_result.append(item) 

【问题讨论】:

标签: python python-3.x pymongo


【解决方案1】:

我能够通过以下方式成功实现我想要做的同样的事情。关键是使用aggregateallowDiskUse 选项。

pipeline = [{"$match" : search_query}]
search_result = mongo_collection.aggregate(pipeline, allowDiskUse=True)
list_search_result = list(search_result)

search_result2 = psd_stream_record.find(search_query)
ascending_search_result = search_result2.sort("time", 1)
oldest_time = ascending_search_result[0]["time"]
descending_search_result = search_result2.sort("time",-1)
latest_time = descending_search_result[0]["time"] 

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 2017-09-13
    • 2011-12-11
    • 2013-01-16
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多