By Ran Bar-Zik | 3/1/2018 | General |Beginners

MongoDB - Indexes

MongoDB - Indexes

In the previous article we spoke about Embedded Documents and their implementation in MongoDB. We also spoke about when to use Embedded Documents and when to use references, with much of the consideration relating to speed and performance. One thing that’s very influential when it comes to speed is the subject of indexes.

 

An index in a database works just like those you may remember in encyclopedias in from childhood. Remember how every encyclopedia had a seperate volume called the index? When you needed to find an entry, you would use the index to find the exact volume and page number, and only then could you find it in the actual encyclopedia. The index allowed us to find entries much more quickly because it was more compact.

 

The same holds true with the index in a database (be it MongoDB or any other). It’s designed to aid us in quickly finding the correct entry. This doesn’t mean we need to run an index on every part of a document. Having too many indexes is unnecessary and likely to cause performance problems, since with each update or insert the database will have to work hard to update all the indexes. But, indexes in specific places, where there are many searches, can be of great help.

 

For example, if we have a document of users, it’s a good idea to make the name of the user an index, since this is a field that will be frequently used many queries. Also, the username is unique. However, it’s not a good idea to turn the user’s screen name into an index since there aren’t a lot of queries are based on it. Further, an index on ‘user description’ would definitely not be necessary since there won’t be many queries on it, and it’s not efficient to make an index of fields that have a large amount of text.

 

Now that we’ve got some background on choosing the right index, the question arises as to how we create them. For examples sake, we can create a collection with 100,000 entries that we can practice on.

for (i = 0; i < 100000; i++) {var title="number "+i;var body = "Page Body Number "+i;    db.pages.insert({"title":title,"body":body});}

You can see that the title is a great choice to use as an index—assuming that our application uses it for data extraction (let’s pretend that’s the case). How do we turn the title into an index?

db.pages.ensureIndex({"title":1})

That’s it. You have to wait a bit (a few seconds) until MongoDB kicks in. If we want, we can make this action run in the background using:

db.pages.ensureIndex({"title":1},{"background":1})

And how do we know that there is an index? Just a quick run of getindexes on the collection will do it.

> db.pages.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.pages"
},
{
"v" : 1,
"key" : {
"title" : 1
},
"name" : "title_1",
"ns" : "test.pages"
}
]

We can see that in addition to the index of the title, which we expected, each collection has an automatic index of the ID. This means that if we use ObjectId to find things, then we already have the index ready to go.

 

In order to erase the index that we made—say we want to make a new one with more possibilities or we just don’t need it—we can use the dropindex method:

db.pages.dropIndex({"title":1})

We can also make a unique index. What does that mean exactly? It means that MongoDB will make each field in a document that we declare as a unique index, unique. Meaning you can’t have others with the same name. How is this done?

db.pages.ensureIndex({"title":1},{"unique":1})

And if we run describe we’ll see:

> db.pages.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.pages"
},
{
"v" : 1,
"unique" : true,
"key" : {
"title" : 1
},
"name" : "title_1",
"ns" : "test.pages"
}
]

See the unique: true? If we try to  insert a document with the same title, we’ll receive this:

> db.pages.insert({"title":"number 62","body":"some body"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.pages.$title_1  dup key: { : \"number 62\" }"
}
})

When we declare an index, there are a few other choices that we can make. Where do these choices appear? In the MongoDB documentation. If you’ve gotten this far in our MongoDB series, then there is no reason not to get to know MongoDB’s excellent documentation. This documentation, that even a trained ape could handle, includes all of the commands you can enter into the console. There you’ll be able to find for example, all of the possibilities for ensureIndex, and much more.

 

In the next article we’ll have a look at GridFS—how to insert binary files into MongoDB. Sounds pretty fun!

 

Previous article: Embedded Documents

 Next article: Using GridFS

 

About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.

By Ran Bar-Zik | 3/1/2018 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now