Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view or modify collation options set on a MongoDB collection?

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!

like image 995
Andrew Nessin Avatar asked Dec 23 '22 09:12

Andrew Nessin


1 Answers

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"
}
like image 88
Stennie Avatar answered May 27 '23 05:05

Stennie