Di MongoDB, $orderBy pengubah kueri mengurutkan hasil kueri dalam urutan menaik atau menurun.
$orderBy menerima dokumen yang menentukan bidang yang akan diurutkan, dan urutan pengurutannya. Urutan pengurutan dapat berupa 1 untuk menaik atau -1 untuk turun.
$orderBy telah ditinggalkan di mongo cangkang sejak v3.2. Gunakan cursor.sort() sebagai gantinya.
Contoh Data
Misalkan kita memiliki koleksi yang disebut pets dengan dokumen sebagai berikut:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 5, "name" : "Bruce", "type" : "Kangaroo", "weight" : 100 }
{ "_id" : 6, "name" : "Hop", "type" : "Kangaroo", "weight" : 130 }
{ "_id" : 7, "name" : "Punch", "type" : "Kangaroo", "weight" : 200 }
{ "_id" : 8, "name" : "Snap", "type" : "Cat", "weight" : 12 }
{ "_id" : 9, "name" : "Ruff", "type" : "Dog", "weight" : 30 } Urutkan dalam Urutan Ascending
Untuk mengurutkan dalam urutan menaik, kami menggunakan 1 untuk pengurutan.
Di bawah ini adalah contoh kueri yang menggunakan $orderBy pengubah kueri untuk mengurutkan koleksi itu berdasarkan weight bidang dalam urutan menaik.
db.pets.find( { $query: {}, $orderBy: { weight: 1 } } ) Hasil:
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 8, "name" : "Snap", "type" : "Cat", "weight" : 12 }
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 9, "name" : "Ruff", "type" : "Dog", "weight" : 30 }
{ "_id" : 5, "name" : "Bruce", "type" : "Kangaroo", "weight" : 100 }
{ "_id" : 6, "name" : "Hop", "type" : "Kangaroo", "weight" : 130 }
{ "_id" : 7, "name" : "Punch", "type" : "Kangaroo", "weight" : 200 } Urutkan dalam Urutan Menurun
Untuk mengurutkan dalam urutan menurun, kami menggunakan -1 untuk pengurutan.
db.pets.find( { $query: {}, $orderBy: { weight: -1 } } ) Hasil:
{ "_id" : 7, "name" : "Punch", "type" : "Kangaroo", "weight" : 200 }
{ "_id" : 6, "name" : "Hop", "type" : "Kangaroo", "weight" : 130 }
{ "_id" : 5, "name" : "Bruce", "type" : "Kangaroo", "weight" : 100 }
{ "_id" : 9, "name" : "Ruff", "type" : "Dog", "weight" : 30 }
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 8, "name" : "Snap", "type" : "Cat", "weight" : 12 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 } Formulir Alternatif
$orderBy pengubah kueri juga dapat digunakan dengan formulir berikut:
db.pets.find()._addSpecial( "$orderby", { weight : 1 } ) Informasi Lebih Lanjut
Seperti disebutkan, $orderBy pengubah kueri telah ditinggalkan di mongo cangkang sejak v3.2. Gunakan cursor.sort() sebagai gantinya.
Lihat dokumentasi MongoDB untuk informasi lebih lanjut.