No MongoDB, o
$orderBy O modificador de consulta classifica os resultados de uma consulta em ordem crescente ou decrescente. $orderBy aceita um documento que especifica o campo a ser classificado e a ordem de classificação. A ordem de classificação pode ser 1 para ascendente ou -1 para descer. $orderBy foi descontinuado no mongo shell desde a v3.2. Use o cursor.sort() método em vez disso. Dados de amostra
Suponha que tenhamos uma coleção chamada
pets com os seguintes 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 } Classificar em ordem crescente
Para classificar em ordem crescente, usamos
1 para a ordem de classificação. Abaixo está um exemplo de uma consulta que usa o
$orderBy modificador de consulta para classificar essa coleção pelo weight campo em ordem crescente. 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 } Classificar em ordem decrescente
Para classificar em ordem decrescente, usamos
-1 para a ordem de classificação. 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 } Um formulário alternativo
O
$orderBy O modificador de consulta também pode ser usado com o seguinte formulário:db.pets.find()._addSpecial( "$orderby", { weight : 1 } ) Mais informações
Como mencionado, o
$orderBy modificador de consulta foi preterido no mongo shell desde a v3.2. Use o cursor.sort() método em vez disso. Consulte a documentação do MongoDB para obter mais informações.