This post explains the collation collations from three aspects. These include an overview, how to use, and operations that support sorting. First, we introduce a summary of the collation
Sorting Rules Overview
Collations provide a set of rules for string comparisons in a particular language habit, for example, in Canadian French, the last accent section of a given vocabulary determines its sort order.
Consider the following French words:
Cote < coté< CTE < cté
Use the Canadian French collation to get the following sort results
Cote < CTE < coté< cté
If no collation is specified, MongoDB uses a simple binary comparison sort. According to this rule, the result of sorting the above words is
Cote < coté< CTE < cté
2. Use of collation rules
When creating a collection and creating an index, we can specify the default collation, or you can specify the collation for the collection and the aggregated crud operations. For operations that support collations, MongoDB uses the default collation if a different collation is not specified.
Collation parameters
' Collation ' + {' locale ' = <string>, ' caselevel ' = <bool>, ' casefirst ' = <string> ' Strength ' + <int>, ' numericordering ' + <bool>, ' alternate ' = <string>, ' Maxvariabl E ' = <string>, ' normalization ' + <bool>, ' backwards ' and <bool>}
The only parameter that must be set is locale. The server converts this parameter to an ICU format locale ID. For example, setting the locale value to en_US
represent American English, Fr_ca on behalf of Canadian French. Full parameter values can be viewed in MongoDB manual entry.
2.1 Specifying collations for collections
The following instance creates a collection of contacts on the test database and assigns it a default locale value of Fr_ca collation. When you create a collection, you specify a collation that ensures that all operations that include queries on the collection contacts use the Fr_ca collation unless the action specifies a specific collation. The index on the new collection also inherits the default collation, unless other collations are specified when the index is created.
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') client[:contacts,{"collation" =>{"locale" = > "Fr_ca"}}]
2.2 Specifying collations for indexes
Specify the collation for the index, you can specify the collation parameter when you create the index, and the following instance creates an index on the Name field field (first_name) named the Address_book collection and sets it to a unique index. The Locale property of the default collation is also set to en_US.
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') Client[:address_book].indexes.create_one ({" First_Name "=>1}, "Unique" =>true, " Collation "=>{ " locale " => " en_US " } )
In order to use this index, you must make sure that the collation is also specified in the query you are using, and the following query uses the index defined above
Client[:address_book].find ({"First_Name" = "Adam"}, "Collation" =>{"locale" = "en_US" })
The following query cannot use the index defined above, the first case is that there is no sort set collation attribute defined, and the second case is that more than one strength attribute is specified on the sorted collection
Client[:address_book].find ({"first_name" + "Adam"}) Client[:address_book].find ({"First_Name" = "Adam"}, "Collation" =>{"locale" = "en_US", "Strength" =>2})
3 operations that support sorting rules
In the MongoDB database, collation is supported for all query, update, and delete methods. Some common methods are listed below:
3.1 Find and Sort methods
A single query can specify collations when querying results and sorting. The following example sorts the query by setting the Locale property of the collation to DE, which sets the query sort using the German-based collation
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> "test") Client[:contacts].find ({"City" = "New York" },{"Collation" =>{"locale" = "De"}}). Sort ({"Name" =>1})
3.2 Find_one_and_update Method
Assume that a collection names contains the following documents:
{"_id": 1, "first_name": "Hans"} {"_id": 2, "first_name": "Gunter"} {"_id": 3, "first_name": "Günter"} {"_id": 4, "first_name": "Jürgen"}
The following find_one_and_update operation does not specify a collation:
Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') doc=client[:names].find_one_and_update ({" First_Name "=>{" $lt "=" Gunter "}},{" $set "=>{" Verified "=>true}})
Due to gunter
is the first word in a collection document, so the result of the query above is empty and no document is updated. The same Find_one_and_update method, but specifies the sort combination, the Locle property is set [email protected]=phonebook
.
collation=phonebook, a vowel-tone character is returned before the vowel-tone character.
client = mongo::client.new ([ "127.0.0.1:27017" &NBSP;], :D atabase => "test") doc = client[:names].find_one_and_update ( { "First_ Name => { $lt => Gunter } }, { $set => { "verified" => true } }, { "collation" => { " Locale " => " [Email protected]=phonebook " },:return_document => :after } )
The results of the above operations are as follows:
{"_id" = 3, "first_name" = "Günter", "verified" and "= True}"
3.2 Find_one_and_update Method
By setting the numericOrdering
parameter to True , you can use the comparison numeric string, which is compared by using the numeric value of the string. For example, the numbers collection contains the following documents:
{"_id": 1, "a": "16"} {"_id": 2, "a": "84"} {"_id": 3, "a": "179"}
The following example finds a document that contains a numeric field with a value greater than 100 and deletes it
Docs=find_one_and_deletes ({"A" =>{"$gt" =>100}},{"collation" =>{"locale" = "End", "numericordering" = >true}})
After you perform the above actions, the file in the
{"_id": 1, "a": "16"} {"_id": 2, "a": "84"}
still exists, but
{"_id": 3, "a": "179"}
has been removed.
But if you do the same thing, you don't use collations. The server will then find the first document with a vocabulary value greater than 100 and delete it.
At this point, the first one in the document is deleted. The results after the query are as follows:
{"_id": 2, "a": "84"} {"_id": 3, "a": "179"}
More than 3.3 delete Delete_many ()
Collation parameters are available for all bulk operations in the Ruby driver. Assume recipes
that the collection contains the following document:
{ "_id" : 1, "dish" : "veggie empanadas", "cuisine" : " Spanish " }{ " _id " : 2, " dish " : " Beef bourgignon ", " cuisine " : "French" }{ "_id" : 3, "dish" : "chicken molé", " Cuisine " : " Mexican " }{ " _id " : 4, " dish " : " chicken Paillard ", " cuisine " : " French " }{ " _id " : 5, " dish " : " Pozole verde ", " cuisine " : " Mexican "&NBSP;}
set strength
is 1 or 2, which allows the server to ignore case when the query filter is running, and the following case uses a query filter that is not case-sensitive to cuisine
field matches the delete operation of the French document .
Client=mongo::cient.new ([' 127.0.0.1:27017 '],:d atabase=> ' test ') Receipes=client[:receipes]docs=delete_many ({" Cusine "=" "French"},{"collation" =>{"locale" = "en_US", "Strength" =>1}})
After executing the above instruction, the documents with _id values of 2 and 4 are deleted.
3.4 Polymerization Aggregation
To aggregate operations on a collection, you need to set the aggregation field for collation. The following aggregation instance uses a collection named names and groups the first_name fields, calculates the number of result documents for each grouping, and sorts them by German phonebook.
Aggregation=names.aggregate ([{"$group" =>{"$_id" = "$first _name", "Name_count" =>{"$sum" =>1}}},{"$sort "=>{" $id "=>1}}],{" collection "=>{" locale "=" [Email Protected]=phonebook "}}) Aggregation.each do |doc| P Docend
This article from "Techfuture" blog, declined reprint!
Ruby Operation MongoDB (Advanced seven)-collation collations