I'm trying to add a default collation to my mongodb collections. It's simple to create a new collection with a collation:
db.createCollection(name, {collation:{locale:"en",strength:1}})
Unfortunately I looked through the docs and didn't see any db.updateCollection
function. How am I supposed to add a collation without destroying and recreating all my documents in a new collection?
MongoDB sorts strings using binary collation by default. The binary collation 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 character values.
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. You can specify collation for a collection or a view, an index, or specific operations that support collation.
Create a Capped CollectionYou must create capped collections explicitly using the db. createCollection() method, which is a mongosh helper for the create command. When creating a capped collection you must specify the maximum size of the collection in bytes, which MongoDB will pre-allocate for the collection.
Each MongoDB collection can have multiple documents. A document is equilant to row in a table in RDBMS. To create a collection, use the db. createCollection() command.
There's one other option that works for my production needs: Execute mongodump
on a collection
mongodump --host hostname --port 32017 --username usr --password pwd --out c:\backup --db my_database --collection my_collection
That will generate two files and one of them named my_collection.metadata.json
. Open this file and modify options
property according to MongoDB docs.
{
"options": {
"collation": {
"locale": "en",
"strength": 1
}
}
...
}
And then restore using mongorestore
mongorestore --host hostname --port 32017 --username usr --password pwd --db contactstore c:\backup\my_database --drop
From then on, any index you create will use that specific collation by default. Unfortunately, this requires a downtime window, so make sure you get one.
From the collation specifications,
After the initial release of server version 3.4, many users will want to apply Collations to all operations on an existing collection. Such users will have to supply the Collation option to each operation explicitly; however, eventually the majority of users wishing to use Collations on all operations on a collection will create a collection with a server-side default. We chose to favor user verbosity right now over abstracting the feature for short-term gains.
So you know its not a option just yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With