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

让人想犯罪 __ 提交于 2021-01-27 14:13:14

问题


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!


回答1:


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"
}



回答2:


Modify default collation is currently in development:

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



来源:https://stackoverflow.com/questions/48840147/how-to-view-or-modify-collation-options-set-on-a-mongodb-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!