Starting from version 2.Four, MongoDB began assisting text indexes to go looking internal string content. The Text Search uses stemming strategies to search for specified phrases in the string fields by means of dropping stemming prevent words like a, an, the, and so on. At present, MongoDB helps around 15 languages.
Enabling Text Search
Initially, Text Search turned into an experimental function but beginning from version 2.6, the configuration is enabled by means of default.
Creating Text Index
Consider the following report below posts series containing the put up textual content and its tags −
> db.posts.insert({
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": ["mongodb", "tutorialspoint"]
}
{
"post_text" : "writing tutorials on mongodb",
"tags" : [ "mongodb", "tutorial" ]
})
WriteResult({ "nInserted" : 1 })
We will create a text index on post_text area in order that we can search interior our posts' text −
>db.posts.createIndex({post_text:"text"})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Using Text Index
Now that we've got created the text index on post_text discipline, we are able to look for all of the posts having the word tutorialspoint in their text.
> db.posts.find({$text:{$search:"tutorialspoint"}}).pretty()
{
"_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"),
"post_text" : "enjoy the mongodb articles on tutorialspoint",
"tags" : [
"mongodb",
"tutorialspoint"
]
}
The above command returned the subsequent end result documents having the word tutorialspoint of their publish text −
{
"_id" : ObjectId("53493d14d852429c10000002"),
"post_text" : "enjoy the mongodb articles on tutorialspoint",
"tags" : [ "mongodb", "tutorialspoint" ]
}
Deleting Text Index
To delete an existing text index, first discover the call of index the use of the following query −
>db.posts.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.posts"
},
{
"v" : 2,
"key" : {
"fts" : "text",
"ftsx" : 1
},
"name" : "post_text_text",
"ns" : "mydb.posts",
"weights" : {
"post_text" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
>
After getting the call of your index from above question, run the following command. Here, post_text_text is the name of the index.
>db.posts.dropIndex("post_text_text")
{ "nIndexesWas" : 2, "ok" : 1 }