WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Text Search


In this chapter we will learn MongoDB - Text Search

Text Search uses stemming techniques to look for specified words in the string fields by dropping stemming stop words like a, an, the, etc. At present, MongoDB supports around 15 languages.

Enabling Text Search

version 2.4, MongoDB support Text Search. But if you are using the previous version of MongoDB, you have to enable text search with the following code
>db.adminCommand({setParameter:true,textSearchEnabled:true})

Creating Text Index

Consider the following document under posts collection containing the post text and its tags
{
   "post_text": "nobel prize 2018 winner physics Gérard Mourou, Arthur Ashkin, Donna Strickland",
   "tags": [
      "physics",
      "Donna Strickland"
   ]
}

We will create a text index on post_text field so that we can search inside our posts' text
>db.posts.ensureIndex({post_text:"text"})

Using Text Index

we will search for all the posts having the word Donna Strickland in their text.
>db.posts.find({$text:{$search:"Donna Strickland"}})

The above command returned the following result documents having the word Donna Strickland in their post text
{
    "_id" : ObjectId("11254h4agr145yth34"),
    "post_text" : "nobel prize 2018 winner physics Gérard Mourou, Arthur Ashkin, Donna Strickland",
    "tags" : [ "physics", "Donna Strickland" ]
}
{
    "_id" : ObjectId("11254h4agr145yth35"),
    "post_text" : "nobel prize 2018 winner physics Gérard Mourou, Arthur Ashkin, Donna Strickland",
    "tags" : [ "physics", "winner" ]
}

If you are using old versions of MongoDB, you have to use the following command
>db.posts.runCommand("text",{search:" winner "})

Deleting Text Index

To delete an existing text index, first find the name of index using the following query >db.posts.getIndexes() After getting the name of your index from above query, run the following command. Here, post_text_text is the name of the index.
>db.posts.dropIndex("post_text_text")





Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.