MongoDBでは、$sort 集約パイプラインステージは、すべての入力ドキュメントを並べ替えて、並べ替えられた順序でパイプラインに返します。
構文
構文は次のようになります:
{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
<sort order>の場所 1にすることができます 昇順の場合、-1 降順の場合、または{ $meta: "textScore" } 計算されたtextScoreで並べ替える 降順のメタデータ。
サンプルデータ
petsというコレクションがあるとします。 次のドキュメントを使用:
{ "_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 } 昇順で並べ替え
昇順で並べ替えるには、1を使用します 並べ替え順序について。
以下は、$sortを使用するクエリの例です。 そのコレクションをweightで並べ替える演算子 昇順のフィールド。
db.pets.aggregate([
{ $sort: { weight: 1 } }
]) 結果:
{ "_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 } 降順で並べ替え
降順で並べ替えるには、-1を使用します 並べ替え順序について。
db.pets.aggregate([
{ $sort: { weight: -1 } }
]) 結果:
{ "_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 } 複数のフィールドで並べ替え
複数のフィールドで並べ替えるには、各フィールド/並べ替え順序の組み合わせをカンマで区切ります。
例
db.pets.aggregate([
{ $sort: { type: 1, weight: -1, _id: 1 } }
]) 結果:
{ "_id" : 8, "name" : "Snap", "type" : "Cat", "weight" : 12 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 9, "name" : "Ruff", "type" : "Dog", "weight" : 30 }
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 7, "name" : "Punch", "type" : "Kangaroo", "weight" : 200 }
{ "_id" : 6, "name" : "Hop", "type" : "Kangaroo", "weight" : 130 }
{ "_id" : 5, "name" : "Bruce", "type" : "Kangaroo", "weight" : 100 }
この例では、typeで並べ替えました フィールドを最初に昇順で、次にweight フィールドを降順で、次に_idで 昇順のフィールド。
つまり、同じ種類のペットが複数いる場合、それらのペットはweightで並べ替えられます。 降順で。同じ種類と体重のペットが複数いる場合、それらのペットは_idで並べ替えられます。 昇順のフィールド。 _idを含めなかった場合 並べ替えプロセスのフィールドを選択すると、同じ種類と体重のペットが任意の順序で表示される可能性があります。これは、クエリを実行するたびに当てはまります。一意のフィールド(_idなど)に並べ替えフィールドがない場合 フィールド)、クエリが実行されるたびに結果が異なる順序で返される可能性があります(可能性もあります)。
さまざまなタイプの並べ替え
さまざまなBSONタイプの値を比較する場合、MongoDBは次の比較順序を使用します(低いものから高いものへ)。
- MinKey(内部タイプ)
- ヌル
- 数値(int、long、double、decimals)
- 記号、文字列
- オブジェクト
- 配列
- BinData
- ObjectId
- ブール値
- 日付
- タイムスタンプ
- 正規表現
- MaxKey(内部タイプ)
次のドキュメントを含むpostsというコレクションがあるとします。
{
"_id" : 1,
"title" : "Web",
"body" : "Create a website with these three easy steps...",
"date" : "2021-01-01T00:00:00.000Z"
}
{
"_id" : 2,
"title" : "Animals",
"body" : "Animals are funny things...",
"date" : ISODate("2020-01-01T00:00:00Z")
}
{
"_id" : 3,
"title" : "Oceans",
"body" : "Oceans are wide and vast...",
"date" : ISODate("2021-01-01T00:00:00Z")
}
最初のdateに注意してください フィールドには日付文字列が含まれていますが、他の2つのドキュメントはDateオブジェクトを使用しています。
また、日付文字列にはドキュメント3とまったく同じ日付が含まれており、この日付はドキュメント2の日付よりも後の日付であることに注意してください。
$sortを適用しましょう dateへ それらのドキュメントのフィールド:
db.posts.aggregate([
{ $sort: { date: 1 } }
]).pretty() 結果:
{
"_id" : 1,
"title" : "Web",
"body" : "Create a website with these three easy steps...",
"date" : "2021-01-01T00:00:00.000Z"
}
{
"_id" : 2,
"title" : "Animals",
"body" : "Animals are funny things...",
"date" : ISODate("2020-01-01T00:00:00Z")
}
{
"_id" : 3,
"title" : "Oceans",
"body" : "Oceans are wide and vast...",
"date" : ISODate("2021-01-01T00:00:00Z")
} この場合、昇順で並べ替えました。つまり、早い日付が最初に来る必要があります。ただし、最初のドキュメントにはDateオブジェクトではなく日付文字列が含まれているため、ドキュメント2の日付よりも日付が遅い場合でも、最初のドキュメントが最初に表示されます。
ここでも、降順です:
db.posts.aggregate([
{ $sort: { date: -1 } }
]).pretty() 結果:
{
"_id" : 3,
"title" : "Oceans",
"body" : "Oceans are wide and vast...",
"date" : ISODate("2021-01-01T00:00:00Z")
}
{
"_id" : 2,
"title" : "Animals",
"body" : "Animals are funny things...",
"date" : ISODate("2020-01-01T00:00:00Z")
}
{
"_id" : 1,
"title" : "Web",
"body" : "Create a website with these three easy steps...",
"date" : "2021-01-01T00:00:00.000Z"
} 繰り返しになりますが、データ型が異なるため、日付の順序は正しくありません。
テキストスコアメタデータの並べ替え
{ $meta: "textScore" }を使用できます $textを使用するときに関連性スコアを降順に並べ替える引数 検索。
例
db.posts.aggregate(
[
{ $match: { $text: { $search: "funny" } } },
{ $sort: { score: { $meta: "textScore" }, title: -1 } }
]
).pretty() 結果:
{
"_id" : 2,
"title" : "Animals",
"body" : "Animals are funny things...",
"date" : ISODate("2020-01-01T00:00:00Z")
} この場合、1つのドキュメントのみがクエリに一致しました。
この例では、{ $meta: "textScore" }で並べ替えました 、次にtitle 降順で。 scoreを使用しました 任意のフィールド名として、ただしこれはクエリシステムによって無視されます。
$textを実行する このような検索では、テキストインデックスを作成する必要があります。そうでない場合は、IndexNotFound エラーが返されます。
グループ化された結果の並べ替え
petsに戻ります コレクションでは、$sortを使用できます $groupの後のステージ 特定のフィールドの値の数でドキュメントのグループを並べ替えるステージ。
db.pets.aggregate([
{
$match: { weight: { $lt: 30 } }
},
{
$group: { _id: "$type", count: { $sum: 1 } }
},
{
$sort : { count : -1 }
}
]) 結果:
{ "_id" : "Cat", "count" : 3 }
{ "_id" : "Dog", "count" : 2 }
ただし、$sortByCountを使用した方がよい場合があります そのような場合の演算子。
詳細情報
詳細については、MongoDBのドキュメントを参照してください。