Làm cách nào để sắp xếp dữ liệu trong mongodb?

Trong MongoDB, khi bạn truy vấn một bộ sưu tập bằng phương thức

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
1, bạn có thể nối thêm phương thức
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2 để chỉ định cách sắp xếp kết quả. Phương thức
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2 chỉ định thứ tự sắp xếp cho con trỏ

Show

Khi sử dụng phương thức

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2, bạn phải cung cấp thứ tự sắp xếp làm tham số. Đây phải là tài liệu JSON xác định thứ tự sắp xếp. Nó thường chứa một hoặc nhiều trường, mỗi trường theo sau bởi một trong hai
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2 hoặc
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
3, tùy thuộc vào việc sắp xếp theo thứ tự tăng dần hay giảm dần

Thí dụ

Trước tiên, hãy thực hiện một truy vấn mà không sử dụng

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2 (để chúng ta có thể thấy thứ tự sắp xếp tự nhiên)

Không có { "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 } { "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" } { "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 } { "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 } { "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 } { "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 } { "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 } { "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 } 2

Kết quả

{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }

Với { "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 } { "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" } { "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 } { "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 } { "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 } { "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 } { "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 } { "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 } 2 theo thứ tự tăng dần

Giá trị

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2 bên cạnh tên trường chỉ định rằng các tài liệu phải được sắp xếp theo trường đã chỉ định theo thứ tự tăng dần

Ở đây chúng tôi sắp xếp kết quả theo tên theo thứ tự tăng dần

Kết quả

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }

Với { "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 } { "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" } { "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 } { "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 } { "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 } { "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 } { "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 } { "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 } 2 theo thứ tự giảm dần

Giá trị

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
3 bên cạnh tên trường chỉ định thứ tự giảm dần

Ở đây chúng tôi sắp xếp kết quả theo tên theo thứ tự giảm dần

Kết quả

{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }

Nhiều trường

Bạn có thể sắp xếp theo nhiều lĩnh vực. Chỉ cần tách từng trường bằng dấu phẩy. Khi bạn thực hiện việc này, các tài liệu sẽ được sắp xếp theo trường đầu tiên được chỉ định, sau đó là trường tiếp theo, v.v.

Ở đây, chúng tôi sắp xếp theo trường công cụ, tiếp theo là trường sinh. Nếu hai hoặc nhiều nhạc sĩ chơi cùng một nhạc cụ, trường sinh sẽ xác định cách chúng được sắp xếp tương đối với nhau

Kết quả

{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }

Và để xem trường thứ hai có thể ảnh hưởng đến thứ tự như thế nào, hãy chuyển nó sang giá trị âm (giảm dần)

Kết quả

________số 8

Sắp xếp có giới hạn

Bạn có thể sử dụng

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
2 với
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
0 để sắp xếp kết quả của tập kết quả giới hạn

Ở đây chúng tôi giới hạn kết quả trong 3 tài liệu

Kết quả

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }

Nếu chúng ta chuyển tên sang thứ tự giảm dần

Kết quả

{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }
0

Lưu ý rằng trong trường hợp này, born không có tác dụng, vì không bao giờ có nhiều hơn một kết quả cho một tên đã cho

So sánh các loại khác nhau

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

Cách sắp xếp hoạt động trong MongoDB?

Thao tác này sắp xếp các tài liệu trong bộ sưu tập người dùng, theo thứ tự giảm dần theo trường tuổi và sau đó theo thứ tự tăng dần theo giá trị trong trường bài đăng. When comparing values of different BSON types, MongoDB uses the following comparison order, from lowest to highest: MinKey (internal type)

MongoDB sử dụng thuật toán sắp xếp nào?

Nếu MongoDB không thể có được thứ tự sắp xếp thông qua quét chỉ mục, thì MongoDB sẽ sử dụng thuật toán sắp xếp top-k . Thuật toán này đệm k kết quả đầu tiên (hoặc cuối cùng, tùy thuộc vào thứ tự sắp xếp) được thấy cho đến nay bởi quyền truy cập bộ sưu tập hoặc chỉ mục cơ bản.

MongoDB có sắp xếp theo mặc định không?

Nếu chúng tôi chưa đặt bất kỳ giá trị nào hoặc ưu tiên cho giá trị cụ thể thì theo mặc định, phương pháp sắp xếp sẽ tự động sắp xếp dữ liệu hoặc kết quả thu thập theo thứ tự tăng dần. Below is the syntax of the MongoDB sort method.

Sắp xếp có đắt trong MongoDB không?

MongoDB thực hiện sắp xếp trong bộ nhớ. Tất cả các tài liệu được lưu trữ trên đĩa được đọc vào RAM, sau đó thuật toán sắp xếp được thực hiện. Sắp xếp trong bộ nhớ không hiệu quả, tốn thời gian & việc sắp xếp một số lượng lớn tài liệu trong bộ nhớ khá tốn kém .