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
You should read it
- Turn off Indexing Service to let the computer run faster
- Projection in MongoDB
- Data type in MongoDB
- MongoDB queries and tasks you should know
- Data modeling in MongoDB
- How to enable / disable network location indexing in Photos app on Windows 10
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- Instructions for indexing on the index on the index in Word
May be interested
- Instructions on 2 ways to install MongoDB on Raspberry Piin 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 MongoDBmongodb 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 2016an 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 MongoDBmongodb supports many different data types. the following are some typical data types.
- MongoDB malicious code attacks more than 26,000 victims in a weekmalware 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 MongoDBto query data from collection in mongodb, you need to use the find () method in mongodb.
- How to write the above index, below index in Excelthis 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 MongoDBin 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 MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
- Shard in MongoDBsharding 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.