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ỏ
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ố 8Sắ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