Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB multiple type of index on same field

Tags:

mongodb

Can I have multiple type of index on same field? Will it affect performance?

Example :

db.users.createIndex({"username":"text"})
db.users.createIndex({"username":1})
like image 493
Swapnil Saurav Avatar asked Mar 16 '17 16:03

Swapnil Saurav


People also ask

Can I have multiple indexes in MongoDB?

MongoDB can use the intersection of multiple indexes to fulfill queries. In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.

Can we create multiple index on same collection in MongoDB?

In order to build multiple indexes at once, you need to use createIndexes() and pass multiple keys into an array.

What is compound indexing in MongoDB?

MongoDB supports compound indexes, where a single index structure holds references to multiple fields [1] within a collection's documents. The following diagram illustrates an example of a compound index on two fields: click to enlarge. [1] MongoDB imposes a limit of 32 fields for any compound index.

How many indexes can be created on a collection in MongoDB?

A collection cannot have more than 64 indexes. The length of the index name cannot be longer than 125 characters. A compound index can have maximum 31 fields indexed.


2 Answers

Yes, you can have different types of indexes on single field. You can create indexes of type e.g text, 2dsphere, hash

You can not create same index with sparse and unique options.

Every write operation is going to update a relevant index entry of all possible types in this case

like image 161
Atish Avatar answered Nov 15 '22 07:11

Atish


The two index options are very different.

When you create a regular index on a string field it indexes the entire value in the string. Mostly useful for single word strings (like a username for logins) where you can match exactly.

A text index on the other hard will tokenize and stem the content of the field. So it will break the string into individual words or tokens, and will further reduce them to their stems so that variants of the same word will match ("talk" matching "talks", "talked" and "talking" for example, as "talk" is a stem of all three). Mostly useful for true text (sentences, paragraphs, etc). Text Search

Text search supports the search of string content in documents of a collection. MongoDB provides the $text operator to perform text search in queries and in aggregation pipelines.

The text search process:

tokenizes and stems the search term(s) during both the index creation and the text command execution. assigns a score to each document that contains the search term in the indexed fields. The score determines the relevance of a document to a given search query.

The $text operator can search for words and phrases. The query matches on the complete stemmed words. For example, if a document field contains the word blueberry, a search on the term blue will not match the document. However, a search on either blueberry or blueberries will match. $regex searches can be used with regular indexes on string fields, to provide some pattern matching and wildcard search. Not a terribly effective user of indexes but it will use indexes where it can: If an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. Further optimization can occur if the regular expression is a “prefix expression”, which means that all potential matches start with the same string. This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range.

http://docs.mongodb.org/manual/core/index-text/

http://docs.mongodb.org/manual/reference/operator/query/regex/

like image 21
sathish anish Avatar answered Nov 15 '22 08:11

sathish anish