问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/44682160/add-default-collation-to-existing-mongodb-collection