Hướng dẫn mongodb count distinct aggregation

Docs HomeMongoDB Manual

Show
    $count
    Hướng dẫn mongodb count distinct aggregation

    Passes a document to the next stage that contains a count of the number of documents input to the stage.

    Note

    $count has the following prototype form:

    <string> is the name of the output field which has the count as its value. <string> must be a non-empty string, must not start with $ and must not contain the . character.

    The $count stage is equivalent to the following $group + $project sequence:

    db.collection.aggregate( [
    { $group: { _id: null, myCount: { $sum: 1 } } },
    { $project: { _id: 0 } }
    ] )

    where myCount would be the output field that contains the count. You can specify another name for the output field.

    Tip

    See also:

    A collection named scores has the following documents:

    { "_id" : 1, "subject" : "History", "score" : 88 }
    { "_id" : 2, "subject" : "History", "score" : 92 }
    { "_id" : 3, "subject" : "History", "score" : 97 }
    { "_id" : 4, "subject" : "History", "score" : 71 }
    { "_id" : 5, "subject" : "History", "score" : 79 }
    { "_id" : 6, "subject" : "History", "score" : 83 }

    The following aggregation operation has two stages:

    1. The $match stage excludes documents that have a score value of less than or equal to 80 to pass along the documents with score greater than 80 to the next stage.

    2. The $count stage returns a count of the remaining documents in the aggregation pipeline and assigns the value to a field called passing_scores.

    db.scores.aggregate(
    [
    {
    $match: {
    score: {
    $gt: 80
    }
    }
    },
    {
    $count: "passing_scores"
    }
    ]
    )

    The operation returns the following results:

    I try to count distinct values in MongoDB, but I'm not getting the results I expect.

    Here is a sample of the data:

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

    I can list all the distinct values using this query:

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

    However, I also need a count. I tried this, but it doesn't give the results I want:

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

    I need to see this:

    SV1: 3
    SV2: 1
    

    MongoDB version:

    > db.version()
    3.0.5
    

    Thanks for any help!