0

I have to create 2 single index on my document but both of the fields are alphanumeric , which one of the below is the right way to create index on alphanumeric fields and why its better than other

Query on this collection will be for exact matching on fieldName.

// fieldName = "abcd1234efgh";

Option 1 -> collection.createIndex(Indexes.ascending(fieldName));
Option 2 -> collection.createIndex(Indexes.text(fieldName));
4
  • 1
    It depends on type of query you are looking for? What kind of search you are going to perform on documents? Can you add more details to your requirements, Commented Jul 20, 2020 at 7:04
  • Thanks @sohan ,update the description with query type Commented Jul 20, 2020 at 15:13
  • Sorry, i did not understand the difference in your updates. Looks same to me. What exactly your search criteria is? is it alphanumeric keywords? Commented Jul 20, 2020 at 15:18
  • yes the query will be on indexing field which is going to be for exact matching and search criteria will be alphanumeric Commented Jul 20, 2020 at 15:39

1 Answer 1

2

As per looking at your requirement I will try to keep this short and simple.

You need to search documents based on alphanumeric text fields .So the order do not matter much here. Even if the indexes are stored in modngoDB based of char sorting. But for you it does not help much.

Most likely we go for text index or regex index types.

db.reviews.createIndex( { comments: "text" } )

MongoDB provides text indexes to support text search queries on string content. text indexes can include any field whose value is a string or an array of string elements.

You can do the same using a regex on a non text-indexed text field, but it would be much slower. More about text index

So, Why is a text index, and its subsequent search faster than a regex on a non-indexed text field? It's because text indexes work as a dictionary, a clever one that's capable of discarding words on a per-language basis (defaults to english). When you run a text search query, you run it against the dictionary, saving yourself the time that would otherwise be spent iterating over the whole collection.

Keep in mind that the text index will grow along with your collection, and it can use a lot of space. I learnt this the hard way when using capped collections. There's no way to cap text indexes.

A regular index on a text field, such as

db.reviews.createIndex( { comments: 1 } )

can be useful only if you search for the whole text. It's used for example to look for alphanumeric hashes. So, From my point It doesn't make any sense to apply this kind of indexes when storing text paragraphs, phrases, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

So if we go with the first approach (collection.createIndex(Indexes.ascending(fieldName));) do you see any issues ? i mean create a single index for the alphanumeric field
and my queries are going to be for the perfect matching of alphanumeric field .
If you look at above explanation again, first approach is meant when you are seaching based on text ,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.Based on your requirement,Also text indexing comes with cost when document are growin. I can suggest with going creating regular index shown in approach 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.