How to perform a full text search in Node.js MongoDB and Mongoose?

Sometimes, we want to perform a full text search in Node.js MongoDB and Mongoose.

In this article, we’ll look at how to perform a full text search in Node.js MongoDB and Mongoose.

How to perform a full text search in Node.js MongoDB and Mongoose?

To perform a full text search in Node.js MongoDB and Mongoose, we create an index on the schema.

For instance, we write

const schema = new Schema({
  name: String,
  email: String,
  profile: {
    something: String,
    somethingElse: String
  }
});

schema.index({
  name: 'text',
  'profile.something': 'text'
});

to call schema.index method to create text indexes on the name and profile.something fields.

As a result, we can do full text search on the name and profile.something fields.

Conclusion

To perform a full text search in Node.js MongoDB and Mongoose, we create an index on the schema.