Add default collation to existing mongodb collection
Add default collation to existing mongodb collection
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?
db.updateCollection
2 Answers
2
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.
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
There's one other option that works for my production needs: Execute mongodump
on a collection
mongodump
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.
my_collection.metadata.json
options
"options":
"collation":
"locale": "en",
"strength": 1
...
And then restore using mongorestore
mongorestore
mongorestore --host hostname --port 32017 --username usr --password pwd --db contactstore c:backupmy_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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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