Index (Mong) in MongoDB

Index (Index) supports the resolution of queries more efficiently. Without an index, MongoDB must scan through all a Collection's Document to select the Documents that connect to the query command. This scan may be ineffective and requires MongoDB to handle a large amount of data.

Indexes are special data structures that store a small part of the data set, making it easier to "dab" into Collection. The index stores the value of a specific field or set of fields, sorted by the value of the field as defined in the index.

The method ensureIndex () in MongoDB

To create an index, you need to use MongoDB's ensureIndex () method.

Syntax

The basic syntax of the ensureIndex () method is as follows:

 > db . COLLECTION_NAME . ensureIndex ({ KEY : 1 }) 

Here, the key is the name of the field you want to create the index and 1 is for the ascending order. To create an index in descending order, you need to use -1.

For example

 > db . mycol . ensureIndex ({ "title" : 1 }) > 

In the ensureIndex () method , you can pass multiple fields, to create indexes on multiple fields, you use:

 > db . mycol . ensureIndex ({ "title" : 1 , "description" :- 1 }) > 

The method ensureIndex () also accepts the list of optional options, listed below:

ParametersTypeBoolean Description Build an index in the Background so that it does not interfere with other database operations. Specify true to build in Background. The default value is false unique Boolean Create a unique index so that the Collection will not accept the insertion of Documents whose key is connecting to an existing value in the index. Specify true to create unique index. The default value is false name String Name of the index. If not specified, MongoDB creates an index name by linking the names of the indexed fields and dropDups Boolean sequencing Create a dropDups index on a field that can have duplicates. MongoDB indexes only the first occurrence of the key and deletes all documents from the Collection that contain the next occurrence of that key. Specify true to create dropDups index. The default value is false sparse Boolean If true, the index only references the Documents with the specified field. These indexes use less space, but operate in different ways in some situations (specifically, sorting). The default value is false expireAfterSeconds Integer Defines a value (in seconds), in the form of a TTL to control how long MongoDB maintains Document in this Collection v Version index Version number of the index. The default version depends on the version of MongoDB running when creating the weights document index A number in the range from 1 to 99999 and denoting the meaning of the relational field to other indexed fields in terms of score default_language String With a text index, this is the language that defines the rules of the index. The default value is english language_override String With a text index, specify the name of the field contained in the Document, the language to load the default language. The default value is language

According to Tutorialspoint

Previous post: Arrange records in MongoDB

Next lesson: Aggregation in MongoDB

5 ★ | 1 Vote

May be interested

  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
  • Query Document in MongoDBQuery Document in MongoDB
    to query data from collection in mongodb, you need to use the find () method in mongodb.
  • How to write the above index, below index in ExcelHow to write the above index, below index in Excel
    this article will guide you to some quick ways to write the above index and lower index in excel for both text and numeric values.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • ObjectId in MongoDBObjectId in MongoDB
    you have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.