'Implementing text search with Mongoose

I want to implement text search on my app, and find documents where the field login matches the searched term,

I did the code below and I have no errors but the result in the route is always an empty array, there must be an error somewhere

Model

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  created_at: {
    type: String,
    required: true,
  },
  description: {
    type: String,
    required: true,
  },
  display_name: {
    type: String,
    required: true,
    index: true,
  },
  login: {
    type: String,
    required: true,
    index: true,
  },
});

UserSchema.index({ login: "text", display_name: "text" });

User = mongoose.model("users", UserSchema);
User.createIndexes();

module.exports = User;

Route

router.post("/users", async function (req, res) {
  const { search } = req.body;
  const users = await User.find({ $text: { $search: search } });
  res.send(users);
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source