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

如果没有文档,MongoDB聚合返回计数为0,mongodb,count,aggregate,Mongodb,Count,Aggregate,我有一个MongoDB查询,它根据日期按5分钟窗口分组并返回count(这是5分钟窗口中使用count:{$sum:1}的文档总数) 如果组中不存在文档,我希望查询还返回特定5分钟窗口的计数0。但是,目前似乎只返回具有正计数的组 当前查询: const cursor = await collection.aggregate([ { $sort : { time : 1 } }, { $match:

我有一个MongoDB查询,它根据日期按5分钟窗口分组并返回count(这是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 }
                }
            }
        ])
预期响应:带有文档计数的时间戳,包括总和0

{ _id: 1525162000000, count: 314 }
{ _id: 1523144100000, count: 0 }
{ _id: 1512155500000, count: 54 }

提前谢谢

免责声明:我不建议在服务器端(所以在MongoDB内部)这样做,而是在客户端处理这种情况

这就是说,这里有一个通用的解决方案,你的问题应该很容易适应你的具体情况

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

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

上述查询的输出将为:

{
    "_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
}

您必须创建伪造的文档,以便分组能够获取它们,这通常会变得有点糟糕。如果可能的话,我会在客户端这样做。如果您发布一些示例数据和预期结果,再加上现有的聚合管道,我们可能能够帮助您。不确定创建虚假文档如何帮助我找出哪些5分钟的windows没有文档。我已经添加了我的查询和预期结果。非常感谢。游乐场:
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
                ]
            }
        }
    }
})
{
    "_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
}