【问题标题】:allowDiskUse not working in pymongoallowDiskUse 在 pymongo 中不起作用
【发布时间】:2016-10-24 12:51:29
【问题描述】:

我在 MongoDB 中存储了以下格式的数据。

{
    "_id" : ObjectId("570b487fb5360dd1e5ef840c"),
    "internal_id" : 1,
    "created_at" : ISODate("2015-07-14T10:08:38.994Z"),
    "updated_at" : ISODate("2016-01-10T00:35:19.748Z"),
    "ad_account_id" : 1,
    "updated_time" : "2013-08-05T04:48:49-0700",
    "created_time" : "2013-08-05T04:46:35-0700",
    "name" : "Sale1",
    "daily": [
                 {"clicks": 5000, "date": "2015-04-16"},
                 {"clicks": 5100, "date": "2015-04-17"},
                 {"clicks": 5030, "date": "2015-04-20"}
             ]
    "custom_tags" : {
        "Event" : {
            "name" : "Clicks"
        },
        "Objective" : {
            "name" : "Sale"
        },
        "Image" : {
            "name" : "43c3fe7b262cde5f476ed303e472c65a"
        },
        "Goal" : {
            "name" : "10"
        },
        "Type" : {
             "name" : "None"
        },
        "Call To Action" : {
             "name" : "None",
        },
        "Landing Pages" : {
            "name" : "www.google.com",
    }
}

我正在尝试按internal_id 对单个文档进行分组,以使用aggregate 方法查找从2015-04-152015-04-21 的总点击次数。

在 pymongo 中,当我尝试在 internal_id 上仅使用 $project 执行 aggregate 时,我得到了结果,但是当我尝试 $project custom_tags 字段时,我得到以下错误:

OperationFailure: Exceeded memory limit for $group, but didn't allow external sort.
Pass allowDiskUse:true to opt in.

在回答here 之后,我什至将聚合函数更改为list(collection._get_collection().aggregate(mongo_query["pipeline"], allowDiskUse=True))。但这仍然会抛出之前的错误。

【问题讨论】:

    标签: mongodb python-2.7 pymongo


    【解决方案1】:

    看看这个链接: Can't get allowDiskUse:True to work with pymongo

    这对我有用:

    someSampleList= db.collectionName.aggregate(pipeline, allowDiskUse=True)
    

    在哪里

    pipeline = [
        {'$sort': {'sortField': 1}},
        {'$group': {'_id': '$distinctField'}}, 
        {'$limit': 20000}]
    

    【讨论】:

      【解决方案2】:

      试试看:

      list(collection._get_collection().aggregate(mongo_query["pipeline"], {allowDiskUse : true}))

      【讨论】:

      • 你的答案不符合python。 allowDiskUse 和 true 没有定义
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2017-03-20
      • 2021-02-02
      相关资源
      最近更新 更多