Những gì bạn cần là nhà điều hành
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Lưu ý: Tôi đã giả sử
0 thành số chứ không phải chuỗi. I have assumed { "_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 }
{ "_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 } |
Output:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 }Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
{ "_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 } |
Passes a document to the next stage that contains a count of the number of documents input to the stage.
Ghi chú
1 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 }
{ "_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 } |
{ "_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 } |
{ "_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 } |
{ "_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 } |
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 } |
{ "_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 } |
{ "_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 } |
{ "_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 } |
{ "_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 } |
{ "_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 } |
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
trong đó
db.scores.aggregate( |
[ |
{ |
$match: { |
score: { |
$gt: 80 |
} |
} |
}, |
{ |
$count: "passing_scores" |
} |
] |
) |
Mẹo
Xem thêm:
Một bộ sưu tập có tên
db.scores.aggregate( |
[ |
{ |
$match: { |
score: { |
$gt: 80 |
} |
} |
}, |
{ |
$count: "passing_scores" |
} |
] |
) |
{ "_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
2 không bao gồm các tài liệu có giá trịdb.scores.aggregate( [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] )
3 nhỏ hơn hoặc bằngdb.scores.aggregate( [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] )
4 để chuyển các tài liệu vớidb.scores.aggregate( [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] )
3 lớn hơndb.scores.aggregate( [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] )
4 cho giai đoạn tiếp theo.db.scores.aggregate( [ { $match: { score: { $gt: 80 } } }, { $count: "passing_scores" } ] ) Giai đoạn
1 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à{ "_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.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:
Giai đoạn đầu tiên: → MongoDB Manual
{ "_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
{ "_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 } |
Giai đoạn thứ hai:usage examples.
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 60 thêm một trường vào đầu ra chứa tổng số bản sao của sách cho mỗi tác giả.
Ghi chúaccumulator expressions.
Hoạt động tổng hợp sau đây xoay vòng dữ liệu trong bộ sưu tập { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 55 để có các tiêu đề được nhóm bởi các tác giả.
Hoạt động trả về các tài liệu sau: does not order its output documents.
Các tài liệu hoạt động tổng hợp sau đây bởi {
"result" : [
{
"_id" : "xyz1",
"countSmaller" : 1,
"countBigger" : 0
},
{
"_id" : "abc1",
"countSmaller" : 2,
"countBigger" : 2
}
],
"ok" : 1
}
57:
8 stage 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 }
{ |
$group: |
{ |
_id: <expression>, // Group key |
<field1>: { <accumulator1> : <expression1> }, |
... |
} |
} |
{ "_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 } |
1{ $group: { _id: <expression>, // Group key <field1>: { <accumulator1> : <expression1> }, ... } }
Giai đoạn thứ hai:See the Group by Null example.
1db.foo.aggregate([ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ])
Không bắt buộc. Tính toán bằng cách sử dụng các toán tử tích lũy.accumulator operators.
Các toán tử
{ |
$group: |
{ |
_id: <expression>, // Group key |
<field1>: { <accumulator1> : <expression1> }, |
... |
} |
} |
db.foo.aggregate([ |
{ |
$sort:{ x : 1, y : 1 } |
}, |
{ |
$group: { |
_id: { x : "$x" }, |
y: { $first : "$y" } |
} |
} |
]) |
db.foo.aggregate([ |
{ |
$sort:{ x : 1, y : 1 } |
}, |
{ |
$group: { |
_id: { x : "$x" }, |
y: { $first : "$y" } |
} |
} |
]) |
Toán tử
db.foo.aggregate([ |
{ |
$sort:{ x : 1, y : 1 } |
}, |
{ |
$group: { |
_id: { x : "$x" }, |
y: { $first : "$y" } |
} |
} |
]) |
Đã thay đổi trong phiên bản 5.0.
| Trả về kết quả của chức năng tích lũy do người dùng xác định. | ||||||||||||||||||||||||||||||||||||||||||||
| Trả về một mảng các giá trị biểu thức duy nhất cho mỗi nhóm. Thứ tự của các phần tử mảng không được xác định. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
| Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
| Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. Mới trong phiên bản 5.2.
| ||||||||||||||||||||||||||||||||||||||||||||
| Có sẵn trong các giai đoạn
Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. Mới trong phiên bản 5.2.
| ||||||||||||||||||||||||||||||||||||||||||||
| Có sẵn trong các giai đoạn
Trả về một tập hợp của các trường
Trả về số lượng tài liệu trong một nhóm. | ||||||||||||||||||||||||||||||||||||||||||||
| Khác biệt với giai đoạn đường ống
Mới trong phiên bản 5.0: Có sẵn trong các giai đoạn
Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
| Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. | ||||||||||||||||||||||||||||||||||||||||||||
| Mới trong phiên bản 5.2. Có sẵn trong các giai đoạn
Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. | ||||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | Mới trong phiên bản 5.2. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. Mới trong phiên bản 5.2.
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 04 | Có sẵn trong các giai đoạn | ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 05 | Trả về một tập hợp của các trường
Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 07 | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 09 | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 11 | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 13 | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Đã thay đổi trong phiên bản 5.0: Có sẵn trong giai đoạn
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 15 | Trả về trung bình của các giá trị số. Bỏ qua các giá trị không phải là số. Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. Mới trong phiên bản 5.2.
| ||||||||||||||||||||||||||||||||||||||||||||
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 18 | Có sẵn trong các giai đoạn
Trả về phần tử dưới cùng trong một nhóm theo thứ tự sắp xếp được chỉ định. Mới trong phiên bản 5.2.
|
Có sẵn trong các 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 } |
db.foo.aggregate([ |
{ |
$sort:{ x : 1, y : 1 } |
}, |
{ |
$group: { |
_id: { x : "$x" }, |
y: { $first : "$y" } |
} |
} |
]) |
{ "_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 } |
{ "_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 } |
Trả về một tập hợp của các trường
db.sales.insertMany([ |
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") }, |
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") }, |
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") }, |
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") }, |
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") }, |
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") }, |
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") }, |
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") }, |
]) |
Trả về số lượng tài liệu trong một nhóm.
Khác biệt với giai đoạn đường ống
{ "_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 } |
{ "_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 } |
Mới trong phiên bản 5.0: Có sẵn trong các 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 } |
db.foo.aggregate([ |
{ |
$sort:{ x : 1, y : 1 } |
}, |
{ |
$group: { |
_id: { x : "$x" }, |
y: { $first : "$y" } |
} |
} |
]) |
{ "_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 } |
db.sales.aggregate( [ |
{ |
$group: { |
_id: null, |
count: { $count: { } } |
} |
} |
] ) |
{ "_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 } |
Trả về một giá trị từ tài liệu đầu tiên cho mỗi nhóm. Đặt hàng chỉ được xác định nếu các tài liệu được sắp xếp.
Khác biệt với toán tử mảng
db.sales.aggregate( [ |
{ |
$group: { |
_id: null, |
count: { $count: { } } |
} |
} |
] ) |
db.foo.aggregate([ |
{ |
$sort:{ x : 1, y : 1 } |
}, |
{ |
$group: { |
_id: { x : "$x" }, |
y: { $first : "$y" } |
} |
} |
]) |
Trả về một tập hợp của các yếu tố
db.sales.insertMany([ |
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") }, |
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") }, |
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") }, |
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") }, |
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") }, |
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") }, |
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") }, |
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") }, |
]) |
db.sales.aggregate( [ |
{ |
$group: { |
_id: null, |
count: { $count: { } } |
} |
} |
] ) |
{ "_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 là giai đoạn đầu tiên trong đường ống.{ "_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 } Tất cả các giai đoạn trước trong đường ống cũng có thể được thực hiện bởi động cơ dựa trên khe.
Để biết thêm thông tin, xem tối ưu hóa
{ "_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 } |
Trong { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 35, hãy tạo một bộ sưu tập mẫu có tên { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 36 với các tài liệu sau:{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 35, create a sample collection named { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 36 with the following documents:
db.sales.insertMany([ |
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") }, |
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") }, |
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") }, |
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") }, |
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") }, |
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") }, |
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") }, |
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") }, |
]) |
Hoạt động tổng hợp sau đây sử dụng 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 } |
{ "_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 } |
db.sales.aggregate( [ |
{ |
$group: { |
_id: null, |
count: { $count: { } } |
} |
} |
] ) |
Hoạt động trả về kết quả sau:
{ "_id" : null, "count" : 8 }
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 0
Mẹo
Hoạt động tổng hợp sau đây sử dụng 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 } |
{ "_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 } |
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 1
Hoạt động trả về kết quả sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 2
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 3
Mẹo{ "_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 } |
db.scores.aggregate( |
[ |
{ |
$match: { |
score: { |
$gt: 80 |
} |
} |
}, |
{ |
$count: "passing_scores" |
} |
] |
) |
Hoạt động trả về kết quả sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 4
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 5
Mẹo
Hoạt động tổng hợp sau đây sử dụng 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 } |
Trong { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 35, hãy tạo một bộ sưu tập mẫu có tên { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 36 với các tài liệu sau:{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 35, create a sample collection named { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 36 with the following documents:
db.sales.insertMany([ |
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("2"), "date" : ISODate("2014-03-01T08:00:00Z") }, |
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : NumberInt("1"), "date" : ISODate("2014-03-01T09:00:00Z") }, |
{ "_id" : 3, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt( "10"), "date" : ISODate("2014-03-15T09:00:00Z") }, |
{ "_id" : 4, "item" : "xyz", "price" : NumberDecimal("5"), "quantity" : NumberInt("20") , "date" : ISODate("2014-04-04T11:21:39.736Z") }, |
{ "_id" : 5, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("10") , "date" : ISODate("2014-04-04T21:23:13.331Z") }, |
{ "_id" : 6, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("5" ) , "date" : ISODate("2015-06-04T05:08:13Z") }, |
{ "_id" : 7, "item" : "def", "price" : NumberDecimal("7.5"), "quantity": NumberInt("10") , "date" : ISODate("2015-09-10T08:43:00Z") }, |
{ "_id" : 8, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : NumberInt("5" ) , "date" : ISODate("2016-02-06T20:20:13Z") }, |
]) |
Hoạt động tổng hợp sau đây sử dụng 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 } |
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 7
Hoạt động trả về kết quả 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 này tương đương với câu lệnh SQL sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 8
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 9
Mẹo
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Hoạt động trả về kết quả sau:
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Mẹo{ "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 35, create a sample collection named { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 55 with the following documents:
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Hoạt động tổng hợp sau đây sử dụng 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 } |
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 41, tính tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Giai đoạn đầu tiên: Giai đoạn ____38 nhóm các tài liệu bằng { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 41 để truy xuất các giá trị vật phẩm riêng biệt. Giai đoạn này trả về { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 44 cho mỗi mục. Giai đoạn thứ hai: Giai đoạn
db.scores.aggregate( |
[ |
{ |
$match: { |
score: { |
$gt: 80 |
} |
} |
}, |
{ |
$count: "passing_scores" |
} |
] |
) |
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Đường ống sau đây tính toán tổng số tiền bán hàng, số lượng bán hàng trung bình và số lượng bán hàng cho mỗi ngày trong năm 2014: uses the { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 59 system variable to group the entire documents by authors. This stage passes the following documents to the next stage:
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
db.scores.aggregate( |
[ |
{ |
$match: { |
score: { |
$gt: 80 |
} |
} |
}, |
{ |
$count: "passing_scores" |
} |
] |
) |
Hoạt động trả về các kết quả sau:
Hoạt động tổng hợp sau đây chỉ định một nhóm
{ |
$group: |
{ |
_id: <expression>, // Group key |
<field1>: { <accumulator1> : <expression1> }, |
... |
} |
} |
Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường { "result" : [ { "_id" : "xyz1", "countSmaller" : 1, "countBigger" : 0 }, { "_id" : "abc1", "countSmaller" : 2, "countBigger" : 2 } ], "ok" : 1 } 41, tính tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:
db.collection.aggregate( [ |
{ $group: { _id: null, myCount: { $sum: 1 } } }, |
{ $project: { _id: 0 } } |
] ) |
Mẹo
Hoạt động tổng hợp sau đây sử dụng 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 } |
Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường {
"result" : [
{
"_id" : "xyz1",
"countSmaller" : 1,
"countBigger" : 0
},
{
"_id" : "abc1",
"countSmaller" : 2,
"countBigger" : 2
}
],
"ok" : 1
}
41, tính tổng số tiền bán cho mỗi mặt hàng và chỉ trả lại các mặt hàng có tổng số tiền bán hàng lớn hơn hoặc bằng 100:
8 operator in a common use case.{ "_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 }