Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *