MongoDB 使用多个ID进行分组

8 投票
2 回答
19525 浏览
提问于 2025-04-18 08:33

我有一组文档,每个文档里有超过20个键,而这些键在不同的文档中可能不一样。有些键可能并不是所有文档都有。我想用MongoDB的聚合框架来进行分组操作。我的查询大概是这样的 -

db.collection.aggregate([{'$group': {'count': {'$sum': 1}, '_id': {'location': '$location', 'type': '$type', 'language': '$language'}}}])

理想情况下,它应该返回那些包含我要求的3个键的文档,并对它们进行“分组”操作。但结果看起来是这样的 -

{
    "result" : [
        {
            "_id" : {
                "location" : "abc",
                "type" : "456"
            },
            "count" : 5
        },
        {
            "_id" : {
                "type" : "123",
                "language" : "english"
            },
            "count" : 1
        },
        {
            "_id" : {
                "location" : "ghi",
                "type" : "9876",
                "language" : "latin"
            },
            "count" : 2
        },
        {
            "_id" : {
                "language" : "hebrew",
                "type" : "9434"
            },
            "count" : 3
        },
        {
            "_id" : {
                "type" : "525",
                "location" : "cari"
            },
            "count" : 1
        },
        {
            "_id" : {
                "language" : "spanish",
                "location" : "dff"
            },
            "count" : 12
        },
        {
            "_id" : {
                "location" : "adpj",
                "type" : "3463",
                            "language": "english"
            },
            "count" : 8
        },
        {
            "_id" : {
                "language" : "french",
                "location" : "nts"
            },
            "count" : 6
        }
    ],
    "ok" : 1
}

问题是,MongoDB即使没有找到我在查询中要求的所有3个键,也会进行分组操作,并显示部分分组结果。我只对那些包含所有键的结果感兴趣。在客户端进行过滤不是一个选项。有没有人能帮帮我?

2 个回答

20

在MongoDB的$group操作中,缺少的值也是一种值。

如果你想排除那些没有所有三个键的文档,可以在你的聚合管道中添加一个$match步骤,这样就能过滤掉那些没有全部键的文档。

 db.collection.aggregate([
     { $match: { 
         "type" : { "$exists" : true}, 
         "location" : { "$exists" : true}, 
         "language" : { "$exists" : true}
       } 
     },
     { $group: {
         "_id": {
             "location": "$location", 
             "type": "$typ", 
             "language": "$language"
         },
         "count": {$sum: 1}
       }
     }
 ]);

示例链接:https://mongoplayground.net/p/WeM_NQlgAHs

2

问题:

db.collection.aggregate([{
    $match: {
        type: {
            "$exists": true
        },
        location: {
            "$exists": true
        },
        language: {
            "$exists": true
        }
    }
}])

撰写回答