Wednesday, February 5, 2014

Text Indexes in MongoDB

Text Indexes are created on fields (columns) which stores string data. Text indexes can be created on one field (column) or multiple fields (Columns). This index is useful when you are searching in the string data type columns.

MongoDB create only one text index per collection (table). Also, Text indexes are case-sensitive in MongoDB.

Before we create Text Indexes we need to enable the textSearchEnabled. To enable the textSearchEnable we need to issue the following command when we are launching mongoDB.

mongod.exe -dbpath "C:\data" --setParameter textSearchEnabled=true

To create Text Index on a field (column) we can use following command:

db.collectio_name.ensureIndex({field_name:"text"})

For example we have created Text Index on Quotes field in famousQuotes collection (table)

db.famousQuotes.ensureIndex({Quotes:"text"})

To create Text Search Index on all string data type fields (columns) of a collection (table), we can use following syntax:

db.Collection-name.ensureIndex({"$**":"text"},{name:IndexName})

For example the following command will create Text search Index on all string data type fields of famousQuotes collection (table)

db.famousQuotes.ensureIndex({"$**":"text"},{name:"QuotesTextSearchIndex"})



Popular Posts

Blog Archive

Real Time Web Analytics