【问题标题】:Counting distinct values in MongoDB计算 MongoDB 中的不同值
【发布时间】:2015-09-28 14:01:37
【问题描述】:

我尝试在 MongoDB 中计算不同的值,但没有得到预期的结果。

以下是数据示例:

{
        "_id" : ObjectId("55d354b0d5cec8aad571b8fe"),
        "version" : "0.4",
        "scan-time" : {
                "start" : 1439913109,
                "end" : 1439913136
        },
        "services" : [
                {
                        "service-type" : "SV1",
                        "service-version" : "V1",
                        "location" : {
                                "ipv4-address" : "192.168.1.1",
                                "protocol" : "tcp"
                        },
                        "classification-method" : "probed"
                },
                {
                        "service-type" : "SV1",
                        "service-version" : "V2",
                        "location" : {
                                "ipv4-address" : "192.168.1.2",
                                "protocol" : "tcp"
                        },
                        "classification-method" : "probed"
                },
                {
                        "location" : {
                                "ipv4-address" : "192.168.1.3",
                                "protocol" : "tcp"
                        },
                        "classification-method" : "probed",
                        "service-type" : "SV1",
                        "service-version" : "V3"
                },
                {
                        "service-type" : "SV2",
                        "service-version" : null,
                        "location" : {
                                "ipv4-address" : "192.168.1.4",
                                "protocol" : "tcp"
                        },
                        "classification-method" : "probed"
                }
        ]
}

我可以使用这个查询列出所有不同的值:

db.collection.distinct("services.service-type")

不过,我还需要数一数。我试过这个,但它没有给出我想要的结果:

db.collection.aggregate(
    [
       {
         $group : {
            _id : "$services.service-type",
            count: { $sum: 1 }
         }
       }
    ]
)

我需要看看这个:

SV1: 3
SV2: 1

MongoDB 版本:

> db.version()
3.0.5

感谢您的帮助!

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    要在不同查询中使用嵌套服务数组,您需要使用 $unwind 展开 $services

    以下查询将输出不同的计数:

    db.collection.aggregate([     
        { $unwind: "$services" },
        {
          $group : {
             _id : { "services-service-type": "$services.service-type" },
             count: { $sum: 1 }
          }
        }
    ])
    

    输出:

    {
        "result" : [ 
            {
                "_id" : {
                    "services-service-type" : "SV2"
                },
                "count" : 1
            }, 
            {
                "_id" : {
                    "services-service-type" : "SV1"
                },
                "count" : 3
            }
        ],
        "ok" : 1
    }
    

    【讨论】:

    • 你不应该在没有$project的情况下$unwind你的文档。
    • 非常感谢,这正是我想要的!
    【解决方案2】:

    首先,您需要 $project 您的文档并返回所有 service-type 的数组以减少您需要在管道中处理的文档的大小,因为 $unwind 复制集合中的每个文档。为此,您使用$map 运算符。然后你可以$unwind返回数组,最后在你的组项目中你使用$sum累加器返回不同service-type的计数

    db.collection.aggregate([
        { "$project": { 
            "services": { 
                "$map": { 
                    "input": "$services", 
                    "as": "s", 
                    "in": "$$s.service-type" 
                }
            }
        }}, 
        { "$unwind": "$services" },
        { "$group": { 
            "_id": "$services",  
            "count": { "$sum": 1 } 
        }}
    ])
    

    返回:

    { "_id" : "SV2", "count" : 1 }
    { "_id" : "SV1", "count" : 3 }
    

    【讨论】:

    • 非常感谢,这正是我想要的!
    猜你喜欢
    • 2020-08-05
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2017-11-19
    • 2017-09-14
    相关资源
    最近更新 更多