Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add default collation to existing mongodb collection

Tags:

mongodb

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?

like image 727
user3413723 Avatar asked Jun 21 '17 17:06

user3413723


People also ask

What is default collation in MongoDB?

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.

What is custom collation in MongoDB?

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.

How do I create a capped collection in MongoDB?

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.

Can a MongoDB have multiple collections?

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.


2 Answers

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.

like image 68
Ostati Avatar answered Oct 13 '22 03:10

Ostati


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.

like image 32
s7vr Avatar answered Oct 13 '22 03:10

s7vr