mongodb在创建索引吗_在MongoDB中建立索引

mongodb在创建索引吗

Indexes in SQL programming are nothing but a special data structure used to easily and quickly locate the record in a given table of the database without being required to traverse through each and every record of the table. Indexes are easily generated using one or more columns of a given table. As a note, the data structure used by an index is a Binary Tree (B-Tree).

SQL编程中的索引不过是一种特殊的数据结构,用于轻松快速地在数据库的给定表中查找记录,而无需遍历表的每条记录。 使用给定表的一个或多个列可以轻松生成索引。 注意,索引使用的数据结构是二叉树 (B-Tree)。

In MongoDB, indexes plays a vital role in efficiently execution of the queries. Basically, if no index is defined in MongoDB, then it has to scan every document of a given collection. Hence, MongoDB uses index to reduce the number of documents to be scanned in a given collection. In fact, MongoDB's index is more or less similar to the indexes used in other relational databases.

在MongoDB中,索引在有效执行查询中起着至关重要的作用。 基本上,如果在MongoDB中未定义索引,则它必须扫描给定集合的每个文档。 因此,MongoDB使用索引来减少给定集合中要扫描的文档数量。 实际上,MongoDB的索引或多或少类似于其他关系数据库中使用的索引。

The fact is that the MongoDB defines the indexes at the collection level and supports indexing on any fields in a MongoDB collection.

事实是,MongoDB在集合级别定义了索引,并支持在MongoDB集合中的任何字段上建立索引。

MongoDB中的默认索引 (Default Index in MongoDB)

Mongodb provides a default index named _id which acts as a primary key to access any document in a collection. This _id index basically avoids the insertion of 2 documents with the same value for the _id field.

Mongodb提供了一个名为_id的默认索引,该索引充当访问集合中任何文档的主键。 该_id索引基本上避免插入_id字段具有相同值的2个文档。

使用createIndex()创建索引 (Creating an Index using createIndex())

To create an index in MongoDB, run the following command :

要在MongoDB中创建索引,请运行以下命令:

db.collection_name.createIndex({field : value })

To create an index on the field regNo for a student collection, run the command db.student.createIndex({regNo : 1})

要在字段regNo为学生集合创建索引,请运行命令db.student.createIndex({regNo : 1})

Following will be the output upon running the above command :

运行上述命令后,将显示以下输出:

{
	"createdCollectionAutomatically": false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
Creating Index in MongoDB

We can also create Index on multiple fields by running a single command. The command will be : db.student.createIndex({regNo : 1, address : -1})

我们还可以通过运行单个命令在多个字段上创建索引。 该命令将是: db.student.createIndex({regNo : 1, address : -1})

Creating Index in MongoDB

MongoDB中的索引类型 (Types of Indexes in MongoDB)

Index TypeDescription
Single field indexUsed to create an index on a single field and it can be a user defined as well apart from the default _id one.
Compound indexMongoDB supports the user-defined indexes on multiple fields.
Multi key indexMongoDB uses multi key indexes basically to store the arrays. MongoDB creates a separate index for each element in an array. MongoDB intelligently identifies to create a multi key index if the index contains elements from an array.
Geospatial indexUsed to support the queries required for the geospatial coordinate data.
Text indexThis index is used to search for a string content in a collection
Hashed indexUsed for hash based Sharding
索引类型 描述
单场索引 用于在单个字段上创建索引,除了默认的_id之外,它还可以是用户定义的。
复合指数 MongoDB在多个字段上支持用户定义的索引。
多键索引 MongoDB基本上使用多键索引来存储数组。 MongoDB为数组中的每个元素创建一个单独的索引。 如果索引包含数组中的元素,则MongoDB会智能识别创建多键索引。
地理空间指数 用于支持地理空间坐标数据所需的查询。
文字索引 该索引用于在集合中搜索字符串内容
散列指数 用于基于哈希的分片

Hence, with all the above features mentioned, Index management is the vital and central part of the MongoDB application.

因此,鉴于上述所有功能,索引管理是MongoDB应用程序的重要组成部分。

翻译自: https://www.studytonight.com/mongodb/indexing-in-mongodb

mongodb在创建索引吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值