Text indexes in MongoDB - Creating, query and dropping text index

You are here : Home / MongoDB Tutorial


Contents of page >
  • 1) Creating Text index in MongoDB>
    • 1.1) create and insert in STUDENT collection, before creating text indexes
    • 1.2) Now, lets create text index on collection >
    • 1.3) Query : Search text in collection using text-index >
    • 1.4) Query : Search phrase using text index >
    • 1.5) Search documents whose indexed field contains "name" but doesn’t include "lmn"
    • 1.6) We can index multiple fields of collection for the text index >
    • 1.7) WILDCARD text index on collection >
  • 2) Creating text index on array-field in collection >
    • 2.1) create and insert in STUDENT collection >
    • 2.2) Now, lets create text index on array-field in collection >
    • 2.3) Query : Search text in array-field using text-index >
  • 3) Drop text index in MongoDB >


1) Creating Text index in MongoDB>
1.1) create and insert in STUDENT collection, before creating text indexes >
db.STUDENT.insert({
 _id: 1,
 X: "first name abc",
 Y: "last name def "
})

db.STUDENT.insert({
 _id: 2,
 X: "first name lmn",
 Y: "last name xyz"
})


IMPORTANT to know : A collection can have maximum of one text index at a time.


1.2) Now, lets create text index on collection >
db.STUDENT.createIndex( { X : "text" } )
We created text index on the field X.


1.3) Query : Search text in collection using text-index >
We will use $text and $search operators. It will search in indexed locations only (i.e. in field X only).
db.STUDENT.find({$text:{$search:"lmn"}})
Output>
{ "_id" : 2, "X" : "first name lmn", "Y" : "last name xyz" }


1.4) Query : Search phrase using text index >
Search documents whose indexed field contains "name" or " lmn"
We will use $text and $search operators.
db.STUDENT.find({$text:{$search:"name lmn"}})
Output>
{ "_id" : 1, "X" : "first name abc", "Y" : "last name def " }
{ "_id" : 2, "X" : "first name lmn", "Y" : "last name xyz" }

1.5) Search documents whose indexed field contains "name" but doesn’t include "lmn". (Basically exclude results with "lmn")
We will use $text and $search operators.
db.STUDENT.find({$text:{$search:"name -lmn"}})
Output>
{ "_id" : 1, "X" : "first name abc", "Y" : "last name def " }


1.6) We can index multiple fields of collection for the text index >
db.STUDENT.createIndex( { X : "text",  Y : "text" } )
We created text index on the fields X and Y.

1.7) WILDCARD text index on collection >
db.STUDENT.createIndex( { "$**" : "text" } )
Wildcard text indexes creates text indexes on multiple fields.



2) Creating text index on array-field in collection >
2.1) create and insert in STUDENT collection >
db.STUDENT.insert({
 _id: 1,
 X: "first name abc",
 PHONE: [ "12", "34" ]
})

db.STUDENT.insert({
 _id: 2,
 X: "first name lmn",
 PHONE: [ "56", "78" ]
})


2.2) Now, lets create text index on array-field in collection >
db.STUDENT.createIndex( { PHONE : "text" } )
We created text index on the array-field PHONE.


2.3) Query : Search text in array-field using text-index >
We will use $text and $search operators. It will search in indexed locations only.
db.STUDENT.find({$text:{$search: "12"}})
Output>
{ "_id" : 1, "X" : "first name abc", "PHONE" : [ "12", "34" ] }


3) Drop text index in MongoDB >


First find the name of text index in collection>
db.STUDENT.getIndexes()
Output >
[
       {
               "v" : 1,
               "key" : {
                       "_id" : 1
               },
               "name" : "_id_",
               "ns" : "mydb.STUDENT"
       },
       {
               "v" : 1,
               "key" : {
                       "_fts" : "text",
                       "_ftsx" : 1
               },
               "name" : "X_text",
               "ns" : "mydb.STUDENT",
               "weights" : {
                       "X" : 1
               },
               "default_language" : "english",
               "language_override" : "language",
               "textIndexVersion" : 2
       }

]
Highlighted X_text is the name of index which we want to delete.


db.STUDENT.dropIndex("X_text");



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

What is MongoDB - A quick introduction to database


eEdit
Must read for you :