Survey

Please help us improve by answering a few questions at our user survey.

Text Search

Use the $text operator to perform text searches on fields which have a text index.

To create a text index on a collection, pass a document containing the name of the field to be indexed with the value ‘text’ to the createIndex() method.

var createTextIndex = function(db, callback) {
  // Get the restaurants collection
  var collection = db.collection('restaurants');
  // Create the index
  collection.createIndex(
    { name : "text" }, function(err, result) {
    console.log(result);
    callback(result);
  });
};

// use the createTextIndex function
var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/test';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");
  createTextIndex(db, function() {
    db.close();
  });
});

The following example assumes that a database called test has a collection called restaurants, with a text index on the name field. A sample dataset is available for download.

var findDocuments = function(db, callback) {
  // Get the documents collection
  var collection = db.collection('restaurants');
  // Find some documents
  collection.find({ '$text': {'$search' : 'Garden' } } ).toArray(function(err, docs) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs);
    callback(docs);
  });      
}

// use the findDocuments() function
var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/test';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");
  findDocuments(db, function() {
    db.close();
  });
});

For more information about the $text operator and its options, see the manual entry.

comments powered by Disqus
On this page