MongoDBでは、cursor.sort() メソッドは、クエリが一致するドキュメントを返す順序を指定します。
sort() メソッドは、ソートするフィールドとソート順を指定するドキュメントを受け入れます。並べ替え順序は、1のいずれかになります。 昇順または-1 降順。
{ $meta: "textScore" }を指定することもできます $textを実行する場合 計算された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.find().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.find().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.find().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 funny 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, but definitely not funny...",
"date" : ISODate("2021-01-01T00:00:00Z")
}
最初のdateに注意してください フィールドには日付文字列が含まれていますが、他の2つのドキュメントはDateオブジェクトを使用しています。
また、日付文字列にはドキュメント3とまったく同じ日付が含まれており、この日付はドキュメント2の日付よりも後の日付であることに注意してください。
dateで並べ替えましょう それらのドキュメントのフィールド:
db.posts.find().sort({ date: 1 }).pretty() 結果:
{
"_id" : 1,
"title" : "Web",
"body" : "Create a funny 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, but definitely not funny...",
"date" : ISODate("2021-01-01T00:00:00Z")
} この場合、昇順で並べ替えました。つまり、早い日付が最初に来る必要があります。ただし、最初のドキュメントにはDateオブジェクトではなく日付文字列が含まれているため、ドキュメント2の日付よりも日付が遅い場合でも、最初のドキュメントが最初に表示されます。
ここでも、降順です:
db.posts.find().sort({ date: -1 }).pretty() 結果:
{
"_id" : 3,
"title" : "Oceans",
"body" : "Oceans are wide and vast, but definitely not funny...",
"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 funny website with these three easy steps...",
"date" : "2021-01-01T00:00:00.000Z"
} 繰り返しになりますが、さまざまなデータ型はそれぞれの中で別々に並べられています。
テキストスコアメタデータの並べ替え
{ $meta: "textScore" }を使用できます $textを使用するときに関連性スコアを降順に並べ替える引数 検索。
例
db.posts.find(
{ $text: { $search: "funny" } },
{ score: { $meta: "textScore" }}
).sort({ score: { $meta: "textScore" } }
).pretty() 結果:
{
"_id" : 2,
"title" : "Animals",
"body" : "Animals are funny things...",
"date" : ISODate("2020-01-01T00:00:00Z"),
"score" : 0.6666666666666666
}
{
"_id" : 3,
"title" : "Oceans",
"body" : "Oceans are wide and vast, but definitely not funny...",
"date" : ISODate("2021-01-01T00:00:00Z"),
"score" : 0.6
}
{
"_id" : 1,
"title" : "Web",
"body" : "Create a funny website with these three easy steps...",
"date" : "2021-01-01T00:00:00.000Z",
"score" : 0.5833333333333334
}
この例では、{ $meta: "textScore" }で並べ替えました 。
MongoDB 4.4から、{ score: { $meta: "textScore" }}の行 オプションです。これを省略すると、scoreが省略されます 結果からのフィールド。したがって、次のことができます(MongoDB 4.4から):
db.posts.find(
{ $text: { $search: "funny" } }
).sort({ score: { $meta: "textScore" } }
).pretty() 結果:
{
"_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, but definitely not funny...",
"date" : ISODate("2021-01-01T00:00:00Z")
}
{
"_id" : 1,
"title" : "Web",
"body" : "Create a funny website with these three easy steps...",
"date" : "2021-01-01T00:00:00.000Z"
}
$textを実行する このような検索では、テキストインデックスを作成する必要があります。そうでない場合は、IndexNotFound エラーが返されます。
詳細情報
詳細については、MongoDBのドキュメントを参照してください。