Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
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"] }, ]]
4THE 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"] }, ]]
4 Giai đoạn tách các tài liệu thành các nhóm theo "khóa nhóm". Đầu ra là một tài liệu cho mỗi khóa nhóm duy nhất.The
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"] }, ]]
4 stage separates documents into groups according to a "group key". The output is one document for each unique group key.Một khóa nhóm thường là một trường, hoặc nhóm các trường. Khóa nhóm cũng có thể là kết quả của một biểu thức. Sử dụng 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"] }, ]]
6 trong giai đoạn đường ố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"] }, ]]
4 để đặt khóa nhóm. Xem dưới đây để biết ví dụ sử dụng.usage examples.Trong đầu ra giai đoạn
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"] }, ]]
4, 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"] }, ]]
6 được đặt thành khóa nhóm cho tài liệu đó.Các tài liệu đầu ra cũng có thể chứa các trường bổ sung được đặt bằng cách sử dụng các biểu thức tích lũy.accumulator expressions.
Ghi chú
4 không đặt hàng tài liệu đầu ra của nó. does not order its output 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"] }, ]]
Giai đoạn
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"] }, ]]
4 có dạng nguyên mẫu sau: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"] }, ]]
4 stage has the following prototype form:{ $group: { _id: , // Group key : { : }, ... } }
| Yêu cầu. Biểu thức | ||||||||||||||||||||||||||||||||||||||||
| 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ử
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"] }, ]]
6 và bộ tích lũy có thể chấp nhận bất kỳ db.sales.aggregate[ [ { $group: { _id: null, count: { $count: { } } } } ] ]
8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.accumulator operators can accept any valid db.sales.aggregate[ [ { $group: { _id: null, count: { $count: { } } } } ] ]
8. For more information on expressions, see Expressions.Toán tử
db.sales.aggregate[ [ { $group: { _id: null, count: { $count: { } } } } ] ]
9 phải là một trong các toán tử tích lũy sau:Đã thay đổi trong phiên bản 5.0.
{ "_id" : null, "count" : 8 } | Yêu cầu. Biểu thức | ||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | 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. Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | Toán tử 9 phải là một trong các toán tử tích lũy sau:Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | Toán tử 9 phải là một trong các toán tử tích lũy sau:Đã thay đổi trong phiên bản 5.0. Tên 2 stages.
| ||||||||||||||||||||||||||||||||||||||||||
{ "_id" : null, "count" : 8 } | Trả về kết quả của chức năng tích lũy do người dùng xác định. Đã thay đổi trong phiên bản 5.0. Tên 2 stages.
| ||||||||||||||||||||||||||||||||||||||||||
SELECT COUNT[*] AS count FROM sales | 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 2.
| ||||||||||||||||||||||||||||||||||||||||||
SELECT COUNT[*] AS count FROM sales | 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. Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
SELECT COUNT[*] AS count FROM sales | Toán tử 9 phải là một trong các toán tử tích lũy sau:Đã thay đổi trong phiên bản 5.0. | ||||||||||||||||||||||||||||||||||||||||||
db.sales.aggregate[ [ { $group : { _id : "$item" } } ] ] | Tên Trả về kết quả của chức năng tích lũy do người dùng xác định. Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
db.sales.aggregate[ [ { $group : { _id : "$item" } } ] ] | Toán tử 9 phải là một trong các toán tử tích lũy sau:Đã thay đổi trong phiên bản 5.0. | ||||||||||||||||||||||||||||||||||||||||||
| Tên Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
| Toán tử 9 phải là một trong các toán tử tích lũy sau:Đã thay đổi trong phiên bản 5.0. Tên 2 and as an expression.
| ||||||||||||||||||||||||||||||||||||||||||
| 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. Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
| Toán tử 9 phải là một trong các toán tử tích lũy sau:Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
| Toán tử 9 phải là một trong các toán tử tích lũy sau:Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
| Toán tử 9 phải là một trong các toán tử tích lũy sau:Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
| Toán tử 9 phải là một trong các toán tử tích lũy sau:Các toán tử 6 và bộ tích lũy có thể chấp nhận bất kỳ 8 hợp lệ nào. Để biết thêm thông tin về biểu thức, xem biểu thức.
| ||||||||||||||||||||||||||||||||||||||||||
| Toán tử 9 phải là một trong các toán tử tích lũy sau:Đã thay đổi trong phiên bản 5.0. Tên 2 stages.
| ||||||||||||||||||||||||||||||||||||||||||
| Trả về kết quả của chức năng tích lũy do người dùng xác định. Đã thay đổi trong phiên bản 5.0. Tên 2
stages.
|
Trả về kết quả của chức năng tích lũy do người dùng xác định.
4 stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit, 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"] }, ]]
4 returns an error. To allow more space for
stage processing, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.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"] }, ]]
Mẹo
Xem thêm:
Phần này mô tả tối ưu hóa để cải thiện hiệu suất của
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"] }, ]]
4. Có những tối ưu hóa mà bạn có thể thực hiện thủ công và tối ưu hóa MongoDB làm cho nội bộ.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"] }, ]]
4. There are optimizations that you can make manually and optimizations MongoDB makes internally.Nếu một đường ống
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
00 và db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
01 theo cùng một trường và giai đoạn 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"] }, ]]
4 chỉ sử dụng toán tử SELECT COUNT[*] AS count FROM sales
6, hãy xem xét thêm một chỉ mục trên trường được nhóm phù hợp với thứ tự sắp xếp. Trong một số trường hợp, giai đoạn 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"] }, ]]
4 có thể sử dụng chỉ mục để nhanh chóng tìm tài liệu đầu tiên của mỗi nhóm.db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
01 by the same field and the 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"] }, ]]
4 stage only uses the SELECT COUNT[*] AS count FROM sales
6
accumulator operator, consider adding an index on the grouped field which matches the sort order. In some cases, the 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"] }, ]]
4 stage can use the index to quickly find the first document of each group.Thí dụ
Nếu một bộ sưu tập có tên
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
05 chứa một chỉ mục db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
06, đường ống sau có thể sử dụng chỉ mục đó để tìm tài liệu đầu tiên của mỗi nhóm:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
Bắt đầu từ phiên bản 5.2, MongoDB sử dụng công cụ truy vấn thực thi dựa trên khe để thực thi các giai đoạn
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"] }, ]]
4 nếu một trong hai: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"] }, ]]
4 stages if either:
4 là giai đoạn đầu tiên trong đường ố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"] }, ]] 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
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"] }, ]]
4.Trong
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
10, hãy tạo một bộ sưu tập mẫu có tên db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11 với các tài liệu sau:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
10, create a sample collection named db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11 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
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"] }, ]]
4 để đếm số lượng tài liệu trong bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11: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"] }, ]]
4 stage to count the number of documents in the db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11 collection: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:
SELECT COUNT[*] AS count FROM sales
Mẹo
Hoạt động tổng hợp sau đây sử dụng giai đoạn
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"] }, ]]
4 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11: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"] }, ]]
4 stage to retrieve the distinct item values from the db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11 collection:db.sales.aggregate[ [ { $group : { _id : "$item" } } ] ]
Hoạt động trả về kết quả sau:
{ "_id" : "abc" } { "_id" : "jkl" } { "_id" : "def" } { "_id" : "xyz" }
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
db.sales.aggregate[ [ // First Stage { $group : { _id : "$item", totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } } } }, // Second Stage { $match: { "totalSaleAmount": { $gte: 100 } } } ] ]
Hoạt động tổng hợp sau đây sử dụng giai đoạn 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"] }, ]]
4 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11: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"] }, ]]
4 stage groups the documents by db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
16 to retrieve the distinct item values. This stage returns the db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
19 for each item.Second Stage:The
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
20 stage filters the resulting documents to only return items with a db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
19 greater than or equal to 100.Hoạt động trả về kết quả sau:
{ "_id" : "abc", "totalSaleAmount" : NumberDecimal["170"] } { "_id" : "xyz", "totalSaleAmount" : NumberDecimal["150"] } { "_id" : "def", "totalSaleAmount" : NumberDecimal["112.5"] }
Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
0Mẹo
Xem thêm:
Trong
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
10, hãy tạo một bộ sưu tập mẫu có tên db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11 với các tài liệu sau:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
10, create a sample collection named db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11 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
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"] }, ]]
4 để đếm số lượng tài liệu trong bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
2Hoạt động trả về kết quả sau: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"] }, ]]
4 stage groups the documents by date and calculates the total sale amount, average
quantity, and total count of the documents in each group.Third Stage:The db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
26 stage sorts the results by the total sale amount for each group in descending order.Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
3Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
4
Hoạt động tổng hợp sau đây sử dụng giai đoạn
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"] }, ]]
4 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
5Hoạt động trả về kết quả sau:
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
6Hoạt động tổng hợp này tương đương với câu lệnh SQL sau:
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
7Hoạt động tổng hợp sau đây sử dụng giai đoạn
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"] }, ]]
4 để truy xuất các giá trị mục riêng biệt từ bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
11:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
10, create a sample collection named db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
30 with the following documents:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
8Các tài liệu của các nhóm hoạt động tổng hợp sau đây theo trường
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
16, tính toán 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ơn hoặc bằng 100:db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
9Giai đoạn đầu tiên: Giai đoạn
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"] }, ]]
4 nhóm các tài liệu bằng db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
16 để truy xuất các giá trị vật phẩm riêng biệt. Giai đoạn này trả về db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
19 cho mỗi mục. Giai đoạn thứ hai: Giai đoạn db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
20 lọc các tài liệu kết quả để chỉ trả về các mục với db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
19 lớn hơn hoặc bằng 100.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"] }, ]]
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:
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"] }, ]]
1Giai đoạn đầu tiên: Giai đoạn db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
20 lọc các tài liệu chỉ chuyển các tài liệu từ năm 2014 sang giai đoạn tiếp theo. Giai đoạn thứ hai: Giai đoạn ____24 nhóm các tài liệu theo ngày và tính tổng số lượng bán, số lượng trung bình và tổng số tài liệu trong các tài liệu trong Mỗi nhóm. Giai đoạn thứ hai: Giai đoạn db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
26 sắp xếp kết quả theo tổng số tiền bán cho mỗi nhóm theo thứ tự giảm dần.Hoạt động trả về các kết quả sau: uses the
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
34 system variable to group the entire documents by authors. This stage passes the following
documents to the next stage: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"] }, ]]
2Hoạt động tổng hợp sau đây chỉ định một nhóm 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"] }, ]]
6 của db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
28, tính toán tổng số lượng bán, số lượng trung bình và số lượng của tất cả các tài liệu trong bộ sưu tập.Trong
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
10, hãy tạo một bộ sưu tập mẫu có tên db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
30 với các tài liệu sau:Hoạt động tổng hợp sau đây xoay vòng dữ liệu trong bộ sưu tập db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
30 để có các tiêu đề được nhóm bởi các tác giả.
db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
Giai đoạn đầu tiên: Giai đoạn
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"] }, ]]
4 nhóm các tài liệu bằng db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
16 để truy xuất các giá trị vật phẩm riêng biệt. Giai đoạn này trả về db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
19 cho mỗi mục. Giai đoạn thứ hai: Giai đoạn db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
20 lọc các tài liệu kết quả để chỉ trả về các mục với db.foo.aggregate[[ { $sort:{ x : 1, y : 1 } }, { $group: { _id: { x : "$x" }, y: { $first : "$y" } } } ]]
19 lớn hơn hoặc bằng 100.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"] }, ]]
3Mẹo
Xem thêm:
Đườ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:
4 operator in a common use case.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"] }, ]]