S
S
source20032016-12-13 23:37:24
MongoDB
source2003, 2016-12-13 23:37:24

What are indexes in Mongodb?

Can you please tell me what indexes are in mongodb, what happens when we declare them, why does search with sorting work faster after that? Is it possible to declare many indexes on a collection?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Wolf, 2016-12-13
@source2003

Indexes in MongoDB are basically the same as indexes in another database. Instead of iterating over a large collection in its entirety, we iterate over an index, which is much easier to cram into memory and work with. Among other things, indexes, unlike the data itself, are more normalized for searching / comparing values.
This is about PostgreSQL indexes, but in the same way, indexes work in all databases that I have worked with.
Once you understand the general purpose of indexes, you can easily find the information you are interested in on a particular type of indexes in a particular database.
The collection is read and the index is built. Usually, in physical form, this is a file (or several files) on a hard disk.
It is possible, but the more indexes, the more data will occupy on disk.
Ideally, the data that you work with directly, for example, the user's "login" in the table/collection of users, falls under the index, because that is what the search is for. All other data outside the index, such as username, password, his phone number, etc. - they are simply attached "to the heap", in the form of non-indexed data, because they are either not searched, or, they are performed quite rarely.

A
Askhat Bikmetov, 2016-12-13
@askhat

Indexes in Monge are the same as in other databases - an ordered field.
Why is it easier to search for them? Take two decks of cards, in one let the cards go suit to suit in order, and shuffle the other. Take a stopwatch and ask two friends to find the queen of clubs, each in their own deck. Share your results in the comments.
Of course, no one will forbid you many indexes, at first it even seems that this is the most logical. But then you will notice that when deleting / adding elements to the collection, you need to rebuild the indexes, and this is a rather resource-intensive operation. So indexes are only needed where you really often search.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question