Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
$sort
Sorts tất cả các tài liệu đầu vào và trả lại chúng cho đường ống theo thứ tự được sắp xếp.Sorts all input documents and returns them to the pipeline in sorted order.
Giai đoạn $sort
có dạng nguyên mẫu sau:$sort
stage has the following prototype form:
{ $sort: { : , : ... } }
$sort
lấy một tài liệu chỉ định [các] trường để sắp xếp và thứ tự sắp xếp tương ứng.
0 có thể có một trong các giá trị sau: takes a document that specifies the field[s] to
sort by and the respective sort order. db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
0 can have one of the following values:
| Sắp xếp tăng dần. | |||||||
| Sắp xếp giảm dần. | |||||||
| Sắp xếp theo siêu dữ liệu được tính toán theo thứ tự giảm dần. Xem Sắp xếp siêu dữ liệu Sắp xếp cho một ví dụ.Text Score Metadata Sort for an example. |
Nếu sắp xếp trên nhiều trường, thứ tự sắp xếp được đánh giá từ trái sang phải. Ví dụ, trong mẫu trên, các tài liệu được sắp xếp đầu tiên bởi
db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
5. Sau đó, các tài liệu có cùng giá trị db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
5 được sắp xếp thêm bởi db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
7.Bạn có thể sắp xếp tối đa 32 phím.
MongoDB không lưu trữ tài liệu trong một bộ sưu tập theo một thứ tự cụ thể. Khi sắp xếp trên một trường chứa các giá trị trùng lặp, các tài liệu chứa các giá trị đó có thể được trả về theo bất kỳ thứ tự nào.
Nếu thứ tự sắp xếp nhất quán được mong muốn, bao gồm ít nhất một trường theo loại của bạn có chứa các giá trị duy nhất. Cách dễ nhất để đảm bảo điều này là bao gồm trường
db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
8 trong truy vấn sắp xếp của bạn.Xem xét bộ sưu tập
db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
9 sau:db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
Lệnh sau sử dụng giai đoạn $sort
để sắp xếp trên trường
db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1:$sort
stage to sort on the db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 field:db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
Trong ví dụ này, thứ tự sắp xếp có thể không nhất quán, vì trường
db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 chứa các giá trị trùng lặp cho cả db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
3 và db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
4. Các tài liệu được trả về theo thứ tự bảng chữ cái bởi db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1, nhưng thứ tự của các tài liệu đó có giá trị trùng lặp cho db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 có thể không giống nhau trên nhiều lần thực hiện cùng loại. Ví dụ: đây là kết quả từ hai lần thực thi khác nhau của lệnh trên:{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
Mặc dù các giá trị cho
db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 vẫn được sắp xếp theo thứ tự bảng chữ cái, thứ tự của các tài liệu chứa các giá trị trùng lặp cho db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 [nghĩa là db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
3 và db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
4] không giống nhau.Để đạt được một loại nhất quán, hãy thêm một trường chứa các giá trị duy nhất độc đáo vào loại. Lệnh sau sử dụng giai đoạn $sort
để sắp xếp trên cả trường
db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 và trường db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
8:$sort
stage to sort on both the db.restaurants.aggregate[ [ { $sort : { borough : 1 } } ] ]
1 field and the db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
8 field:db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
Do trường
db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
8 luôn được đảm bảo chứa các giá trị duy nhất, nên thứ tự sắp xếp được trả về sẽ luôn giống nhau trên nhiều lần thực hiện cùng loại.Đối với trường hoặc trường sắp xếp, đặt thứ tự sắp xếp thành
db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
1 hoặc db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
2 để chỉ định sắp xếp tăng dần hoặc giảm dần, như trong ví dụ sau:db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
Hoạt động này sắp xếp các tài liệu trong bộ sưu tập
{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
7, theo thứ tự giảm dần theo trường { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
8 và sau đó theo thứ tự tăng dần theo giá trị trong trường { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
9.Khi so sánh các giá trị của các loại BSON khác nhau, MongoDB sử dụng thứ tự so sánh sau, từ thấp nhất đến cao nhất:
Miney [loại nội bộ]
Vô giá trị
Số [ints, longs, đôi, số thập phân]
Biểu tượng, chuỗi
Sự vật
Mảng
Bindata
ID đối tượng
Boolean
Ngày
Dấu thời gian
Biểu hiện thông thường
MaxKey [loại nội bộ]
Để biết chi tiết về thứ tự so sánh/sắp xếp cho các loại cụ thể, hãy xem thứ tự so sánh/sắp xếp.
Đối với một đường ống bao gồm tìm kiếm
db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
0, bạn có thể sắp xếp bằng cách giảm điểm số liên quan bằng cách sử dụng biểu thức db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
1. Trong tài liệu db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
2, đặt biểu thức db.restaurants.insertMany[ [ { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"}, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"}, { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"}, { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"}, { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"}, ] ];
3 thành một tên trường tùy ý. Tên trường bị bỏ qua bởi hệ thống truy vấn. Ví dụ:db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
Hoạt động này sử dụng toán tử
db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
0 để khớp với các tài liệu và sau đó sắp xếp đầu tiên theo siêu dữ liệu db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
5 theo thứ tự giảm dần, và sau đó theo trường { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" } { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" } { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" } { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" } { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
9 theo thứ tự giảm dần. Tên trường db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
7 trong tài liệu sắp xếp bị bỏ qua bởi hệ thống truy vấn. Trong đường ống này, siêu dữ liệu db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
5 không được bao gồm trong phép chiếu và không được trả lại như một phần của các tài liệu phù hợp. Xem db.restaurants.aggregate[ [ { $sort : { borough : 1, _id: 1 } } ] ]
9 để biết thêm thông tin.Khi $sort
đi trước
db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
1 và không có giai đoạn can thiệp nào sửa đổi số lượng tài liệu, trình tối ưu hóa có thể hợp nhất db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
1 vào $sort
. Điều này cho phép hoạt động $sort
chỉ duy trì kết quả db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 hàng đầu khi nó tiến triển, trong đó db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 là giới hạn được chỉ định và đảm bảo rằng MongoDB chỉ cần lưu trữ các mục db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 trong bộ nhớ. Tối ưu hóa này vẫn được áp dụng khi db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
8 là db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
9 và các mục db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 vượt quá giới hạn bộ nhớ tổng hợp.$sort
precedes a
db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
1 and there are no intervening stages that modify the number of documents, the optimizer can coalesce the db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
1 into the
$sort
. This allows the $sort
operation to only maintain the top db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 results as it progresses, where db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 is the specified limit, and ensures that MongoDB only needs to store db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 items in
memory. This optimization still applies when db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
8 is db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
9 and the db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
5 items exceed the aggregation memory limit.Tối ưu hóa có thể thay đổi giữa các bản phát hành.
$sort
phải tuân theo giới hạn sử dụng bộ nhớ 100 megabyte, nhưng có thể viết các tệp tạm thời vào đĩa nếu cần thêm không gian.
Bắt đầu từ MongoDB 6.0, các giai đoạn đường ống yêu cầu hơn 100 megabyte bộ nhớ để thực hiện ghi các tệp tạm thời vào đĩa theo mặc định. Trong các Verison trước đó của MongoDB, bạn phải chuyển
db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
2 cho cá nhân db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
3 và db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
4 để cho phép hành vi này.Các lệnh cá nhân
db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
3 và db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
4 có thể ghi đè tham số db.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
7 bằng một trong hai:Sử dụng
2 để cho phép viết các tệp tạm thời ra đĩa khidb.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
7 được đặt thànhdb.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ] $sort
0Sử dụng
$sort
1 để cấm ghi các tệp tạm thời ra đĩa khi
7 được đặt thànhdb.users.aggregate[ [ { $match: { $text: { $search: "operating" } } }, { $sort: { score: { $meta: "textScore" }, posts: -1 } } ] ]
9db.users.aggregate[ [ { $sort : { age : -1, posts: 1 } } ] ]
Mẹo
Xem thêm:
Nhà điều hành $sort
có thể tận dụng một chỉ mục nếu nó được sử dụng trong giai đoạn đầu tiên của đường ống hoặc nếu nó chỉ được đặt trước bởi giai đoạn $sort
5.$sort
operator can take
advantage of an index if it's used in the first stage of a pipeline or if it's only preceeded by a $sort
5 stage.
Khi bạn sử dụng $sort
trên cụm Sharded, mỗi Shard sẽ sắp xếp các tài liệu kết quả của nó bằng cách sử dụng một chỉ mục có sẵn. Sau đó, $sort
7 hoặc một trong những mảnh vỡ thực hiện một loại hợp nhất được phát trực tuyến.$sort
on a sharded cluster, each shard sorts its result documents using an index
where available. Then the $sort
7 or one of the shards performs a streamed merge sort.