Hướng dẫn count distinct mongodb - đếm mongodb riêng biệt

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

Show
    $count

    Hướng dẫn count distinct mongodb - đếm mongodb riêng biệt

    Chuyển một tài liệu cho giai đoạn tiếp theo có chứa số lượng tài liệu nhập vào giai đoạn.

    Ghi chú

    $count có mẫu nguyên mẫu sau: has the following prototype form:

    { "_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 }
    0 là tên của trường đầu ra có số lượng là giá trị của nó.
    { "_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 }
    0 phải là một chuỗi không trống, không được bắt đầu với
    { "_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 }
    2 và không được chứa ký tự
    { "_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 }
    3.

    Giai đoạn $count tương đương với trình tự

    { "_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 }
    5 +
    { "_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 }
    6 sau:$count stage is equivalent to the following
    { "_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 }
    5 +
    { "_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 }
    6 sequence:

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

    trong đó

    { "_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 }
    7 sẽ là trường đầu ra chứa số lượng. Bạn có thể chỉ định một tên khác cho trường đầu ra.

    Mẹo

    Xem thêm:

    Một bộ sưu tập có tên

    { "_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 }
    8 có các tài liệu sau:

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

    Hoạt động tổng hợp sau đây có hai giai đoạn:

    1. Giai đoạn

      { "_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 }
      9 không bao gồm các tài liệu có giá trị
      db.scores.aggregate(
      [
      {
      $match: {
      score: {
      $gt: 80
      }
      }
      },
      {
      $count: "passing_scores"
      }
      ]
      )
      0 nhỏ hơn hoặc bằng
      db.scores.aggregate(
      [
      {
      $match: {
      score: {
      $gt: 80
      }
      }
      },
      {
      $count: "passing_scores"
      }
      ]
      )
      1 để chuyển các tài liệu với
      db.scores.aggregate(
      [
      {
      $match: {
      score: {
      $gt: 80
      }
      }
      },
      {
      $count: "passing_scores"
      }
      ]
      )
      0 lớn hơn
      db.scores.aggregate(
      [
      {
      $match: {
      score: {
      $gt: 80
      }
      }
      },
      {
      $count: "passing_scores"
      }
      ]
      )
      1 cho giai đoạn tiếp theo.

    2. Giai đoạn $count trả về số lượng các tài liệu còn lại trong đường ống tổng hợp và gán giá trị cho một trường gọi là

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

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

    Hoạt động trả về các kết quả sau:

    Tôi cố gắng đếm các giá trị riêng biệt trong MongoDB, nhưng tôi không nhận được kết quả mà tôi mong đợi.

    Đây là một mẫu dữ liệu:

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

    Tôi có thể liệt kê tất cả các giá trị riêng biệt bằng truy vấn này:

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

    Tuy nhiên, tôi cũng cần một số lượng. Tôi đã thử điều này, nhưng nó không cho kết quả tôi muốn:

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

    Tôi cần phải thấy điều này:

    SV1: 3
    SV2: 1
    

    Phiên bản MongoDB:

    > db.version()
    3.0.5
    

    Cảm ơn vì bất kì sự giúp đỡ!