How to Create a MongoDB Index如何创建MongoDB索引

Posted on: 13/07/2018 (last updated: 04/08/2021) by Thomas Zahn

In this post, we’ll take a look at how to create a MongoDB index and modify existing ones using Studio 3T’s Index Manager.在本文中,我们将了解如何创建MongoDB索引,并使用Studio 3T的索引管理器修改现有索引。

Don’t have Studio 3T on your machine yet? Download the latest version, available for Windows, Mac, and Linux.你的机器上还没有Studio 3T吗?下载最新版本,适用于Windows、Mac和Linux。

Add a MongoDB index添加MongoDB索引

In Studio 3T, connect to your MongoDB server. 在Studio 3T中,连接到MongoDB服务器You’ll then see the databases and their collections in the left-hand Collections pane. 然后,您将在左侧的“集合”窗格中看到数据库及其集合。

Simply right-click on the collection you want to create a new index for and choose “Add Index…” from the pop-up menu, as shown below.只需右键单击要为其创建新索引的集合,然后从弹出菜单中选择“添加索引…”,如下所示。

This will open the “Add Index” view.这将打开“添加索引”视图。

At the top, enter the name of your index (if you leave this field empty, a default index name will be generated for you out of the fields and their index types).在顶部,输入索引的名称(如果您将此字段留空,将在字段及其索引类型中为您生成默认索引名称)。

In the first tab, named “Fields”, you can then add the fields that you want indexed and assign to each of them the desired index type.在第一个名为“字段”的选项卡中,可以添加要编制索引的字段,并为每个字段指定所需的索引类型。

Click “Add Field(s)…” to select the index fields.单击“添加字段…”以选择索引字段。

Studio 3T does a quick schema scan over a small set of documents in your collection to present you with a tree of available fields in your collection.Studio 3T对集合中的一小部分文档进行快速模式扫描,以显示集合中可用字段的树。

Select the fields for your index from the tree, or, in case the schema scan didn’t cover your desired field, enter the field name directly, and click “Add”.从树中选择索引字段,或者,如果架构扫描没有覆盖所需字段,直接输入字段名,然后单击“添加”。

After you have added your index fields, they will appear with the default index type “ascending”.添加索引字段后,它们将以默认索引类型“升序”显示。

You can easily change the index type for each field by double-clicking (or hitting ENTER) in its corresponding index type cell.通过双击(或按ENTER键)相应的索引类型单元格,可以轻松更改每个字段的索引类型。

Simply choose the desired index type from the drop-down list.只需从下拉列表中选择所需的索引类型。

MongoDB index optionsMongoDB索引选项

At the bottom of the “Fields” tab, you can select various general index options.在“字段”选项卡的底部,可以选择各种常规索引选项。

One of those options that you should always carefully consider is the “Create in Background” option.其中一个你应该经常仔细考虑的选项是“在后台创建”选项。

By default, MongoDB will build indexes in the foreground. 默认情况下,MongoDB将在前台构建索引。This will result in compact index sizes and can take significantly less time than building the index in the background.这将产生紧凑的索引大小,并且比在后台构建索引所需的时间要少得多。

However, the huge disadvantage of this approach is that the entire database will be locked for read and write operations while the index is building in the foreground. 然而,这种方法的巨大缺点是,当索引在前台构建时,整个数据库将被锁定以进行读写操作。For any live database, this is a serious consideration.对于任何实时数据库来说,这都是一个严肃的考虑。

Therefore, although background-built indexes will usually take longer to build and will initially result in less compact index sizes (they will converge over time towards the compactness of foreground-built indexes, though), we generally recommend setting the background option.因此,尽管后台构建的索引通常需要更长的时间来构建,并且最初会导致索引大小不那么紧凑(但随着时间的推移,它们会向前台构建的索引的紧凑程度收敛),但我们通常建议设置后台选项。

Text index options文本索引选项

In the “Text Options”, you can set the index options that are specific to text indexes.在“文本选项”中,可以设置特定于文本索引的索引选项。

For example, click the “Default Language” combo box to choose your desired default language of your text index fields.例如,单击“默认语言”组合框,选择文本索引字段所需的默认语言。

In the “Fields Weights” table, you can weigh your text index fields in relation to one another.在“字段权重”表中,您可以根据文本索引字段之间的关系进行权重计算。

All text index fields will automatically be added with a default weight of “1”. 所有文本索引字段将自动添加默认权重“1”。To change a field’s weight, just double-click the value (or hit ENTER in the selected cell) and overwrite the weight as you see fit.要更改字段的权重,只需双击该值(或在选定单元格中按ENTER键),然后根据需要覆盖权重。

You can also manually add weight fields by clicking “Add Weight(s)…”. 也可以通过单击“添加权重…”手动添加权重字段。However, this will usually only be necessary if you have previously selected the “$**” field option to index all text fields.但是,通常只有在之前选择“$**”字段选项为所有文本字段编制索引时,才需要这样做。

Geo index options地理索引选项

Analogously, in the “Geo Options” tab, you can set index options specific to the different Geo index types.类似地,在“地理选项”选项卡中,可以设置特定于不同地理索引类型的索引选项。

Partial index options部分索引选项

Under “Partial Index Options” you can choose to create a partial index, so that it only indexes documents that meet your JSON filter expression.在“部分索引选项”下,您可以选择创建一个部分索引,以便它只对符合JSON筛选器表达式的文档进行索引。

You also get the chance to validate your JSON before creating the partial index.您还可以在创建部分索引之前验证JSON。

Overwrite JSON code覆盖JSON代码

Studio 3T also lets you – if you so desire – manually overwrite the actual JSON code that will be sent to the MongoDB server for the index creation.Studio 3T还允许您手动覆盖实际的JSON代码,该代码将被发送到MongoDB服务器进行索引创建。

Any changes you make here to the JSON code will of course be parsed and automatically set in the other tabs accordingly!当然,您在这里对JSON代码所做的任何更改都将被解析,并自动在其他选项卡中相应地设置!

Use custom collation使用自定义排序规则

Under the last tab “Collation”, you can use custom collation by choosing values for the predefined settings.在最后一个选项卡“排序规则”下,可以通过为预定义设置选择值来使用自定义排序规则。

What’s collation again? Read more about it here. 什么是排序规则?在这里阅读更多关于它的信息。

Once you have configured your index, click “Create Index” and the index will be built on the server.配置索引后,单击“创建索引”,索引将在服务器上生成。

You may close the index view tab at any point, the index build will continue on the server.您可以随时关闭“索引视图”选项卡,索引生成将在服务器上继续。

Modify a MongoDB index修改MongoDB索引

In MongoDB, it is not really possible to modify an existing index. 在MongoDB中,实际上不可能修改现有索引。Instead, what one essentially needs to do is drop the original index and re-create it from scratch.实际上,我们需要做的是删除原始索引并从头开始重新创建它。

Luckily, Studio 3T makes this process completely transparent for you.幸运的是,Studio 3T让这个过程对您来说完全透明。

To modify an existing index, expand your collection node in your collection list to see all its indexes.要修改现有索引,请展开集合列表中的集合节点以查看其所有索引。

Select the index you would like to modify, right-click it and choose “Modify Index…” from the pop-up menu.选择要修改的索引,右键单击它,然后从弹出菜单中选择“修改索引…”。

Studio 3T will then retrieve all information about that index and open the familiar index view with all parameters filled already filled in.然后,Studio 3T将检索有关该索引的所有信息,并打开熟悉的索引视图,其中已填写了所有参数。

Of course, you can edit the parameters under the other tabs (e.g. Text Options, Geo Options) and also manually overwrite the JSON code again.当然,您可以在其他选项卡(例如文本选项、地理选项)下编辑参数,也可以再次手动覆盖JSON代码。

To re-create the index with the modifications you have made, simply click “Re-create Index”.要使用所做的修改重新创建索引,只需单击“重新创建索引”。

We hope that we’ve convinced you how easy it is to add and modify a MongoDB index using Studio 3T.我们希望我们已经说服您,使用Studio 3T添加和修改MongoDB索引是多么容易。

Download Studio 3T下载Studio 3T and be on your way to faster query performance.并且可以提高查询性能。


Want to take indexing to another level?想把索引提升到另一个层次吗?