13

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?

user3413723
  • 11,147
  • 6
  • 55
  • 64
  • That doesn't do what I am looking for. It updates all documents in the collection that match the query using the specified collation. What I want to do is set the default collation for the collection that will be used all the time by default. – user3413723 Jun 21 '17 at 17:17
  • MongoDb developers are working on this feature. [See](https://jira.mongodb.org/browse/SERVER-35314). – A'' May 01 '19 at 17:03

2 Answers2

10

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.

Ostati
  • 4,623
  • 3
  • 44
  • 48
  • Production needs? Really? – Amol M Kulkarni Jun 03 '20 at 15:02
  • 2
    Yes, it happened once, when collation change was needed due to case sensitivity rules changes. – Ostati Jun 03 '20 at 15:54
  • 1
    This happens in production mode for me, too. In our case, we support "sorting" in a later version of our product. So we need to add "collation" now, which wasn't required in previous versions. Hope export/import will work. I'll give a +1 in that case. Thx! Just one remark - the library we use doesn't support collation to be added to queries, so that's not an option.... – Radiesel Sep 30 '20 at 05:05
  • This is the best solution I have found so far, but it doesn't work with my unique field indexes. I ended up dropping every documents and letting mongoose create a new collection from scratch, but this is not production-friendly. – BiasInput Oct 05 '22 at 20:09
9

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.

s7vr
  • 73,656
  • 11
  • 106
  • 127
  • Good find. Gee, so strange that you can't do this! Oh well good thing I'm in beta so I can recreate the collections easily! – user3413723 Jun 21 '17 at 17:59