如果没有文档,MongoDB 聚合返回计数为 0

Ale*_*a G 6 aggregate count mongodb

我有一个 MongoDB 查询,它根据日期按 5 分钟窗口进行分组并返回计数(这是使用 5 分钟窗口中的文档总数count: { $sum: 1 })。

如果该组中不存在文档,我希望查询也为特定的 5 分钟窗口返回 0 计数。但目前看来,仅返回计数为正的组。

当前查询:

        const cursor = await collection.aggregate([
            { $sort : { time : 1 } },
            { 
                $match: {
                     $and: [ 
                        {selector: string },
                        {time: {$gte: timestamp }}
                     ]
                }
            },
            { 
                $group: {
                    _id: {
                        $subtract: [
                            { $subtract: [ "$time", 0 ] },
                            { $mod: [ 
                                { $subtract: [ "$time", 0 ] },
                                1000 * 60 * 5
                            ]}
                        ],
                    },
                    count: { $sum: 1 }
                }
            }
        ])
Run Code Online (Sandbox Code Playgroud)

预期响应:包含文档计数(包括总和 0)的时间戳

{ _id: 1525162000000, count: 314 }
{ _id: 1523144100000, count: 0 }
{ _id: 1512155500000, count: 54 }
Run Code Online (Sandbox Code Playgroud)

提前致谢!

dni*_*ess 4

免责声明:我不建议在服务器端(因此在 MongoDB 内部)执行此操作,而是在客户端处理这种情况。

也就是说,这是针对您的问题的通用解决方案,它应该可以轻松适应您的具体情况。

假设您有以下文档(或来自示例中的聚合管道的输出):

{
    "category" : 1
}
{
    "category" : 1
}
// note the missing { category: 2 } document here
{
    "category" : 3
}
Run Code Online (Sandbox Code Playgroud)

以下管道将创建空存储桶(因此字段中的值范围中缺少的“间隙”值的计数为 0 的文档category- 在本例中为数字 2):

var bucketSize = 1;

db.getCollection('test').aggregate({
    $group: {
        _id: null, // throw all documents into the same bucket
        "min": { $min: "$category" }, // just to calculate the lowest
        "max": { $max: "$category" }, // and the highest "category" value 
        "docs": { $push: "$$ROOT" } // and also keep the root documents
    }
}, {
    $addFields: {
        "docs": { // modify the existing docs array - created in the previous stage
            $concatArrays: [ // by concatenating
                "$docs", // the existing docs array
                {
                    $map: { // with some other array that will be generated
                        input: {
                            $range: [ "$min", "$max", bucketSize ] // based on the min and max values and the bucket size
                        },
                        as: "this",
                        in: { // but represented not as a plain number but as a document that effectively creates a bogus document
                            "category": "$$this", // the bogus category will be set to the respective value
                            "bogus": 1 // marker that allows us not to count this document in the next stage and still get a bucket from $group
                        }
                    }
                }
            ]
        }
    }
}, {
    $unwind: "$docs" // flatten the "docs" array which will now contain the bogus documents, too
}, {
    $group: {
        _id: "$docs.category", // group by category
        "count": { // this is the result we are interested in
            $sum: { // which will be aggregated by calculating the sum for each document of
                $cond: [ // either 0 or 1 per document
                    { $eq: [ "$docs.bogus", 1 ] }, // depending on whether the document should count as a result or not
                    0,
                    1
                ]
            }
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

上述查询的输出将是:

{
    "_id" : 2,
    "count" : 0.0 // this is what we wanted to achieve
}
{
    "_id" : 3,
    "count" : 1.0 // correct number of matches
}
{
    "_id" : 1,
    "count" : 2.0 // correct number of matches
}
Run Code Online (Sandbox Code Playgroud)