En MongoDB, el $orderBy
modificador de consulta ordena los resultados de una consulta en orden ascendente o descendente.
$orderBy
acepta un documento que especifica el campo a ordenar y el orden de clasificación. El orden de clasificación puede ser 1
para ascender o -1
por descender.
$orderBy
ha quedado en desuso en el mongo
cáscara desde v3.2. Usa el cursor.sort()
en su lugar.
Datos de muestra
Supongamos que tenemos una colección llamada pets
con los siguientes documentos:
{ "_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 }
Ordenar en orden ascendente
Para ordenar en orden ascendente, usamos 1
para el orden de clasificación.
A continuación se muestra un ejemplo de una consulta que utiliza $orderBy
modificador de consulta para ordenar esa colección por el weight
campo en orden ascendente.
db.pets.find( { $query: {}, $orderBy: { weight: 1 } } )
Resultado:
{ "_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 }
Ordenar en orden descendente
Para ordenar en orden descendente, usamos -1
para el orden de clasificación.
db.pets.find( { $query: {}, $orderBy: { weight: -1 } } )
Resultado:
{ "_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 }
Una forma alternativa
El $orderBy
el modificador de consulta también se puede utilizar con el siguiente formulario:
db.pets.find()._addSpecial( "$orderby", { weight : 1 } )
Más información
Como se mencionó, el $orderBy
el modificador de consulta ha quedado obsoleto en mongo
cáscara desde v3.2. Usa el cursor.sort()
en su lugar.
Consulte la documentación de MongoDB para obtener más información.