Collations — Go
Docs Menu

Docs HomeGo

照合

このガイドでは、照合を使用して、クエリまたは集計操作の結果を string 値で並べ替える方法を学習できます。 照合は、特定の言語とロケールに適用される文字の順序付け規則のセットです。

MongoDB sorts strings using binary collation by default. This collation method uses the ASCII standard character values to compare and order strings. Certain languages and locales have specific character ordering conventions that differ from the ASCII standard.

たとえば、カナダフランス語では、他の文字が同じ場合、string の順序を決定します。 次のカナダフランス語の単語について考えてみます。

  • コピー

  • coté

  • côte

  • côté

デフォルトのバイナリ照合を使用する場合、MongoDB はそれらを次の順序でソートします。

cote
coté
côte
côté

カナダ フランス語の照合を使用する場合、MongoDB はそれらを次の順序でソートします。

cote
côte
coté
côté

To specify a collation, create a Collation object. You must define the Locale field of the Collation object; all other fields are optional. For example, the following code snippet specifies a Collation object with the "en_US" locale collation:

myCollation := &options.Collation{Locale: "en_US"}

For a complete list of Collation object fields, visit the Collation API documentation. To see all the supported locales and the default values for the Locale fields, visit Supported Languages and Locales.

新しいコレクションまたはビューを作成するときに照合を適用できます。 これにより、そのコレクションまたはビューで呼び出される操作のデフォルトの照合方法が定義されます。 CreateCollectionOptionsまたはCreateViewOptionsオブジェクトを介して照合を設定します。 次に、オプション オブジェクトを引数としてCreateCollection()またはCreateView()メソッドを呼び出します。

次の例では、 booksという新しいコレクションを作成し、 "fr"ロケールでデフォルトの照合を指定しています。 Strength照合フィールドには、文字のアクセントの違いを無視するための1の値があります。

myCollation := &options.Collation{Locale: "fr", Strength: 1}
opts := options.CreateCollection().SetCollation(myCollation)
err := db.CreateCollection(context.TODO(), "books", opts)
if err != nil {
panic(err)
}

If you call an operation that uses a collation on the books collection, the operation will now use the default collation specified in the Create a Collection Example.

booksコレクションには次のドキュメントが含まれているとします。

{"name" : "Emma", "length" : "474"}
{"name" : "Les Misérables", "length": "1462"}
{"name" : "Infinite Jest", "length" : "1104"}
{"name" : "Cryptonomicon", "length" : "918"}
{"name" : "Ça", "length" : "1138"}

注意

ドキュメントを挿入する方法については、「 ドキュメントの挿入 」を参照してください

次の例では、 Find()メソッドを使用して、 "Infinite Jest"のアルファベット順に先行するname値を持つすべてのドキュメントを返します。

filter := bson.D{{"name", bson.D{{"$lt", "Infinite Jest"}}}}
cursor, err := coll.Find(context.TODO(), filter)
if err != nil {
panic(err)
}
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
for _, result := range results {
fmt.Println(result)
}

デフォルトのbooks照合を指定しない場合、 Find()メソッドはデフォルトのバイナリ照合ルールに従って、 "Infinite Jest"に先行するname値を決定します。 これらのルールでは、"$" で始まる単語が、"I" で始まる単語の後に配置されます。 出力は、次のようになります。

[{name Cryptonomicon} {length 918}]
[{name Emma} {length 474}]

Find()メソッドの詳細については、「データの取得 」を参照してください。

コレクションに新しいインデックスを作成するときに、照合を適用できます。 インデックスはコレクション内のドキュメントの順序付けられた表現を保存するため、MongoDB インスタンスはメモリ内でソート操作の順序付けを実行する必要がありません。

To use the index in an operation, your operation must use the same collation as the one specified in the index. Additionally, ensure that the operation is covered by the index that contains the collation. Set a collation through an IndexOptions object and call the CreateOne() method with your options object as an argument.

After creating the books collection and applying a default collation, as shown in the コレクションの作成例 section, you cannot change the collection's default collation. However, you can create an index for the collection with a different collation.

次の例では、 CreateOne()メソッドを使用してnameフィールドに昇順のインデックスを作成し、 "en_US"ロケールで新しい照合を指定します。

myCollation := &options.Collation{Locale: "en_US"}
opts := options.Index().SetCollation(myCollation)
indexModel := mongo.IndexModel{
Keys: bson.D{{"name", 1}},
Options: opts,
}
name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
panic(err)
}
fmt.Println("Name of Index Created: " + name)

コレクションからドキュメントを読み取り、更新、削除する操作では、照合を使用できます。 操作に照合を適用すると、コレクションに対して以前に定義されたデフォルトの照合は上書きされます。

If you apply a new collation to an operation that differs from an index's collation, you cannot use that index. As a result, the operation may not perform as well as one that is covered by an index. For more information on the disadvantages of sorting operations not covered by an index, see Using Indexes to Sort Query Results. See the MongoDB manual for a list of operations that support collation.

照合をサポートする操作を使用して、 booksコレクション内のドキュメントを更新およびクエリできます。

The following example uses the Find() method to return documents with length values greater than "1000". The NumericOrdering collation field has a value of true to ensure that values are sorted in numerical order rather than alphabetical order:

filter := bson.D{{"length", bson.D{{"$gt", "1000"}}}}
myCollation := &options.Collation{Locale: "en_US", NumericOrdering: true}
opts := options.Find().SetCollation(myCollation)
cursor, err := coll.Find(context.TODO(), filter, opts)
if err != nil {
panic(err)
}
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
for _, result := range results {
fmt.Println(result)
}

Without specifying a collation with a NumericOrdering field set to true, the same Find() operation would compare length values as strings. For example, the operation would consider the string "824" as greater than "1000". The output would resemble the following:

[{name Emma} {length 474}]
[{name Cryptonomicon} {length 918}]

Find()メソッドの詳細については、「データの取得」ガイドを参照してください。

照合の詳細については、次のマニュアル ページを参照してください。

このガイドで説明されているメソッドの詳細については、次の API ドキュメントを参照してください。

フィードバックを送る