Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
$count
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:
Giai đoạn
9 không bao gồm các tài liệu có giá trị{ "_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 nhỏ hơn hoặc bằngdb.scores.aggregate[ [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] ]
1 để chuyển các tài liệu vớidb.scores.aggregate[ [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] ]
0 lớn hơndb.scores.aggregate[ [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] ]
1 cho giai đoạn tiếp theo.db.scores.aggregate[ [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] ] 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à
5.db.scores.aggregate[ [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] ]
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 đỡ!