6

How do you modify the options that we set on a collection at the time of creation? And how do we view the options already set?

If we see the collation option for example, I create a collection like this:

db.createCollection("words", {collation : {locale :"es", strength : 2}});

And add few documents:

db.words.insertOne({text : "Résumé"});
db.words.insertOne({text : "Resume"});
db.words.insertOne({text : "résumé"});
db.words.insertOne({text : "resume"});

How do I change the strength of the collation on this collection to 3? how do I see the change? I do not see any relevant functions available on the db object or db.words object or in the docs!

Andrew Nessin
  • 1,206
  • 2
  • 15
  • 22

2 Answers2

10

How do you modify the options that we set on a collection at the time of creation?

As at MongoDB 3.6, the default collation options can only be specified when a collection is created. There is no support for modifying the default collation options.

However, if you want to use collation options other than the default you can specify a collation document for operations that support collation, such as find() and aggregate().

how do we view the options already set?

There are several approaches.

The db.getCollectionInfos() shell helper displays additional collection information such as collation defaults:

db.getCollectionInfos({name:'words'})[0].options.collation
{
  "locale": "es",
  "caseLevel": false,
  "caseFirst": "off",
  "strength": 2,
  "numericOrdering": false,
  "alternate": "non-ignorable",
  "maxVariable": "punct",
  "normalization": false,
  "backwards": false,
  "version": "57.1"
}

You can also check the default collation options used by the query planner:

> db.words.find().explain().queryPlanner.collation
{
  "locale": "es",
  "caseLevel": false,
  "caseFirst": "off",
  "strength": 2,
  "numericOrdering": false,
  "alternate": "non-ignorable",
  "maxVariable": "punct",
  "normalization": false,
  "backwards": false,
  "version": "57.1"
}
Stennie
  • 63,885
  • 14
  • 149
  • 175
  • Running those "view" examples dont seem to show me what collation would be applied to a collection that is created in the db, but rather what collation is applied to existing collections. Am I reading that correctly? – StingyJack Feb 18 '21 at 04:02
4

Modify default collation is currently in development:

https://jira.mongodb.org/browse/SERVER-35314

Holgrabus
  • 141
  • 2
  • 9
  • While [SERVER-35314 (Allow changing default collation for a collection)](https://jira.mongodb.org/browse/SERVER-35314) is definitely a relevant feature request to watch & upvote in the MongoDB Jira issue tracker, note the current status (Jan 2019) is "backlog" rather than "in development". It is not yet clear if or when that work might be scheduled. – Stennie Jan 27 '19 at 05:54