Full-Text Search in Node JS with Mongoose
For $text
queries to work, MongoDB needs to index the field with a text index. To create this index by mongoose use
fields: {type: [String], text: true}
See here for the MongoDB documentation of text indexes.
You need to add a text index to your schema like below:
userSchema.index({fields: 'text'});
Or use userSchema.index({'$**': 'text'});
if you want to include all string fields