Text Index in MongoDB


Text index supports text search queries on string content. We can any add fields which value is string or an array of string elements.
Text index is introduced in MongoDB version 3 but some key features added in MongoDB version 3.2, the new features are:

1-   Case sensitive: Previous versions of the text index are case insensitive for [A-z] only; i.e. case insensitive for non-diacritics Latin characters only. For all other characters, earlier versions of the text index treat them as distinct.

2-  Diacritic Insensitivity: There are no facility in the index to distinguish between characters that contain diacritic marks and no marks the corresponding item for example é, ê, and e.

3-  Tokenization Delimiters: In previous version text index uses the delimiters categorized under dash and hyphen. For example: if given a string "Il a dit qu'il «était le Meilleur joueur du monde»", the text index treats «, », and spaces as delimiters.

Create text index: Suppose we have a collection “Inventory of books”


{"_id":1, "Title":"title1", "Author":"author1", "Publisher":"publisher1", "Qty":10, "Price":20.02}
{"_id":2, "Title":"title2", "Author":"author2", "Publisher":"publisher2", "Qty":12, "Price":26.00}
{"_id":3, "Title":"title3", "Author":"author3", "Publisher":"publisher3", "Qty":14, "Price":20.07}
{"_id":4, "Title":"title4", "Author":"author4", "Publisher":"publisher4", "Qty":11, "Price":243.08}
{"_id":5, "Title":"title5", "Author":"author5", "Publisher":"publisher5", "Qty":14, "Price":202.00}
{"_id":6, "Title":"title6", "Author":"author6", "Publisher":"publisher6", "Qty":16, "Price":102.02}
{"_id":7, "Title":"title7", "Author":"author7", "Publisher":"publisher7", "Qty":13, "Price":130.02}
{"_id":8, "Title":"title8", "Author":"author8", "Publisher":"publisher8", "Qty":15, "Price":233.02}
{"_id":9, "Title":"title9", "Author":"author9", "Publisher":"publisher9", "Qty":16, "Price":244.02}
{"_id":10, "Title":"title10", "Author":"author1", "Publisher":"publisher1", "Qty":17, "Price":250.02}
{"_id":11, "Title":"title11", "Author":"author2", "Publisher":"publisher2", "Qty":13, "Price":260.02}
{"_id":12, "Title":"title12", "Author":"author3", "Publisher":"publisher3", "Qty":13, "Price":267.02}
{"_id":13, "Title":"title13", "Author":"author4", "Publisher":"publisher4", "Qty":12, "Price":288.02}
{"_id":14, "Title":"title14", "Author":"author5", "Publisher":"publisher5", "Qty":14, "Price":286.02}
{"_id":15, "Title":"title15", "Author":"author6", "Publisher":"publisher6", "Qty":16, "Price":50.02}
{"_id":16, "Title":"title16", "Author":"author7", "Publisher":"publisher7", "Qty":17, "Price":60.02}


Create text index using the following query


db.books.createIndex( { Title: "text", Author: "text",Publisher:"text" } )


Result:


/* 1 */
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1.0
}



Note: A collection can have most on text index.

No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...