How to Use MongoDB TextSearch by Mongoose

2014/8/22 min read
bookmark this
Responsive image

This blog shows example code of how to use MongoDB's text search by use Mongoose. You can also find What's Mongoose Text Search at here.

How to use MongoDB text search with Mongoose

First, install following npm module.

mongoose-text-search

textSearch = require('mongoose-text-search')

set up your schema

// give schema text search capabilities
yourMongooseSchema.plugin(textSearch);
// add a text index
yourMongooseSchema.index(
{
    Title: 'text'
});

Following is example how to use text search.


var option = {
        // list of schema property you do not want to include
        project: '-DealId -DealSiteId -Title'  
        // casts queries based on schema             
        , filter: { likes: { $gt: 1000000 }} 
        , limit: 10
        , language: 'spanish'
          , lean: true
    };

var keyWord = 'key word to search'
yourCollection.textSearch(keyWord , option, function (err, output)
{
}

More about Mongoose with MongoDB text search, check mongoose-text-search.

Mongodb need to be 2.4 or higher

Other References

  1. Blog -MongoDB Text Search Explained
  2. Enable Text Search
  3. MongoDB release note 3.0
  4. MongoDB release note 3.2
  5. $text
  6. Text Searching with MongoDB

Other Search Engine

Want to use other popular search engine?