如何使一个方面聚合输出为 true key:value json?

How do I make a facet aggregation output into true key:value json?

我写了一个脚本来聚合一些数据,但输出不正确 json。

我尝试修改聚合管道的 $project 部分,但我认为我做的不对。

    pipeline = [
        {
            "$match": {
                "manu": {"$ne": "randomized"},
            }},
        {
            "$match": {
                "rssi": {"$lt": "-65db"}
            }
        },
        {"$sort": {"time": -1}},
        {
            "$group": {"_id": "$mac",
                       "lastSeen": {"$first": "$time"},
                       "firstSeen": {"$last": "$time"},
                       }
        },

        {
            "$project":
            {
                "_id": 1,
                "lastSeen": 1,
                "firstSeen": 1,
                "minutes":
                {
                    "$trunc":
                    {
                        "$divide": [{"$subtract": ["$lastSeen", "$firstSeen"]}, 60000]
                    }
                },

            }
        },

        {
            "$facet": {
                "0-5": [
                    {"$match": {"minutes": {"$gte": 1, "$lte": 5}}},
                    {"$count": "0-5"},
                ],
                "5-10": [
                    {"$match": {"minutes": {"$gte": 5, "$lte": 10}}},
                    {"$count": "5-10"},
                ],
                "10-20": [
                    {"$match": {"minutes": {"$gte": 10, "$lte": 20}}},
                    {"$count": "10-20"},
                ],
            }
        },
        {"$project": {
            "0-5": {"$arrayElemAt": ["[=12=]-5.0-5", 0]},
            "5-10": {"$arrayElemAt": ["-10.5-10", 0]},
            "10-20": {"$arrayElemAt": ["-20.10-20", 0]},

        }},

        {"$sort": SON([("_id", -1)])}

    ]

    data = list(collection.aggregate(pipeline, allowDiskUse=True))

所以我得到的输出基本上是 {'0-5': 2914, '5-10': 1384, '10-20': 1295} - 不能用于迭代。

理想情况下应该是这样的

{'timeframe': '0-5', 'count': 262}

有什么建议吗?

提前致谢。

您可以尝试以下聚合(替换您当前的 $facet 及以下阶段):

db.col.aggregate([{
    "$facet": {
            "0-5": [
                {"$match": {"minutes": {"$gte": 1, "$lte": 5}}},
                {"$count": "total"},
            ],
            "5-10": [
                {"$match": {"minutes": {"$gte": 5, "$lte": 10}}},
                {"$count": "total"},
            ],
            "10-20": [
                {"$match": {"minutes": {"$gte": 10, "$lte": 20}}},
                {"$count": "total"},
            ]
        },
    },
    {
        $project: {
            result: { $objectToArray: "$$ROOT" }
        }
    },
    {
        $unwind: "$result"
    },
    {
        $unwind: "$result.v"
    },
    {
        $project: {
            timeframe: "$result.k",
            count: "$result.v.total"
        }
    }
])

$facet returns single document that contains three fields (results of sub-aggregations). You can use $objectToArray to get it in a shape of k and v fields and then use $unwind 每个键获取单个文档。