Advanced index operation in MongoDB

You follow the following Document of users collection:

 { "address" : { "city" : "Los Angeles" , "state" : "California" , "pincode" : "123" }, "tags" : [ "music" , "cricket" , "blogs" ], "name" : "Tom Benzamin" } 

The above document contains a Subdocument as address and an array of tags .

Index array fields in MongoDB

Suppose that we want to search the user document based on tags. To do this, we will create an index on the array of tags in that Collection.

Create an index on the array ie create separate indexes for each of its fields. So in this situation, when we create an index on the array of tags, individual indexes will be created for their values: music, cricket and blogs.

To create an index on the array of tags, you use:

 > db . users . ensureIndex ({ "tags" : 1 }) 

After creating the index, we can perform a search on that Collection's tags field like this:

 > db . users . find ({ tags : "cricket" }) 

To check if a valid index has been used, you use the explain command.

 > db . users . find ({ tags : "cricket" }). explain () 

The explain command returns the result in "cursor": "BtreeCursor tags_1" which confirms that the logical index is used.

Field indexing is the Subdocument

Assume that you want to search in Documents based on city, state, and pincode fields. When all these fields are part of the address field, which is the Subdocument, you will create an index on all the fields of that Subdocument.

To create an index on all the fields of that Subdocument, you use:

 > db . users . ensureIndex ({ "address.city" : 1 , "address.state" : 1 , "address.pincode" : 1 }) 

Once the index has been created, you can search for any field in the fields of that Subdocument. Take advantage of this index as follows:

 > db . users . find ({ "address.city" : "Los Angeles" }) 

Remember, the query expression must follow the order of the specified index. Therefore, the index created above will support the following queries:

 > db . users . find ({ "address.city" : "Los Angeles" , "address.state" : "California" }) 

It will also support the following query:

 > db . users . find ({ "address.city" : "LosAngeles" , "address.state" : "California" , "address.pincode" : "123" }) 

According to Tutorialspoint

Previous article: Atomic Operation in MongoDB

Lesson: Limit of index in MongoDB

4 ★ | 1 Vote

May be interested

  • Instructions on 2 ways to install MongoDB on Raspberry PiInstructions on 2 ways to install MongoDB on Raspberry Pi
    in this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
  • Atomic Operation in MongoDBAtomic Operation in MongoDB
    mongodb does not support atomic transaction over multiple documents. however, it provides atomic operations on a single document. therefore, if a document has hundreds of fields, the update command will either update all those fields or not update any fields, thus maintaining atomicity at the document level.
  • Use Index in Access 2016Use Index in Access 2016
    an index is a pointer to each value that appears in the indexed table / column, which has the same meaning as the entries in the index of a book.
  • 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.