【问题标题】:MongoDB aggregate query adding to pipeline causing no resultsMongoDB聚合查询添加到管道导致没有结果
【发布时间】:2018-08-20 06:08:02
【问题描述】:

我正在通过 PyMongo 运行聚合。

聚合格式很好,如下所示:

[{
    $match: {
        syscode: {
            $in: [598.0]
        },
        date: {
            $gte: newDate(1509487200000),
            $lte: newDate(1510264800000)
        }
    }
},
{
    $group: {
        _id: {
            date: "$date",
            start_date: "$start_date",
            end_date: "$end_date",
            daypart: "$daypart",
            network: "$network"
        },
        syscode_data: {
            $push: {
                syscode: "$syscode",
                cpm: "$cpm"
            }
        }
    }
}]

当我在 Python 中对其光标使用 .explode 方法时,它不返回任何结果。

当我通过 NoSQL Booster for MongoDB 运行它时,我得到了结果。也就是说,当我通过 PyMongo 运行 Mongo 日志文件时,它并没有改变。

当我查看 Mongo 日志时,其中添加了一个额外的按管道分组。显然助推器知道如何处理这个问题,而我不知道。

{ $group: { _id: null, count: { $sum: 1.0 } } }

这是我看到的完整日志行。

2018-03-11T21:05:04.374+0200 I COMMAND  [conn71] command Customer.weird_stuff command: aggregate { aggregate: "rate_cards", pipeline: [ { $match: { syscode: { $in: [ 598.0 ] }, date: { $gte: new Date(1509487200000), $lte: new Date(1510264800000) } } }, { $group: { _id: { date: "$date", start_date: "$start_date", end_date: "$end_date", daypart: "$daypart", network: "$network" }, syscode_data: { $push: { syscode: "$syscode", cpm: "$cpm" } } } }, { $group: { _id: null, count: { $sum: 1.0 } } } ], cursor: { batchSize: 1000.0 }, $db: "Customer" } planSummary: COLLSCAN keysExamined:0 docsExamined:102900 cursorExhausted:1 numYields:803 nreturned:1 reslen:134 locks:{ Global: { acquireCount: { r: 1610 } }, Database: { acquireCount: { r: 805 } }, Collection: { acquireCount: { r: 805 } } } protocol:op_query 122ms

发生了什么事?如何从 Python 端处理这个问题?

我正在挖掘时的注意事项:当我幸运并在 Pymongo 中使用无序字典(默认)时,此管道就会运行。当我通过 JSON.Jsondecoder 运行输入 JSON 时:

json.JSONDecoder(object_pairs_hook=OrderedDict).decode(parsed_param) 

输出具有非常复杂的格式(由于管道需要维护其顺序,这是必要的)并最终传递了额外的部分。

【问题讨论】:

    标签: json mongodb pymongo ordereddictionary aggregates


    【解决方案1】:

    因此,我缺乏兴趣找到了解决方法。检查问题后,我发现当我向管道({"$sort": {"_id": 1}}) 添加一个额外的步骤时,从 Python 字典到 Mongo JSON 聚合的翻译并没有生成额外的 JSON 对象。

    这是一个糟糕的答案,但我认为根本原因是在这个特定环境中复杂的有序字典和 Mongo JSON 查询之间的转换有一个影响这个特定查询的小错误。

    我很高兴能找到它并进一步检查它,但我埋头于一份新工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2020-05-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多