0

I am making a website in which there is a function which will take an input and search for the text on mongoDB. I want to print all the user names which have such texts in their data.

I have tried looking up the official documentation and making changes to find a solution but nothing has helped so far.

// Search
router.post('/search', ensureAuthenticated, (req, res) => {
    const { name } = req.body;
    console.log(name);
    //I want to get the username and that specific data printed which is inside an array called books
    const Results = User.find( { $text: { $search: name } } );
    console.log(Results);
    res.redirect('borrow');
});

The actual result is nothing as I am not able to progress any further. The expected result is to get an multi-dimensional array which will have the user and the corresponding book.

For example if I have 2 people User 1 has books: Harry Potter Angels and Demons Shy Harry

User 2 has books: Harry Bad

If I search for harry then I want to store all the users which harry along with the books.

  • You'll need to update the question with some sample data and sample output that you want to achieve. Have a look at [**$regex**](https://docs.mongodb.com/manual/reference/operator/query/regex/) and this [https://stackoverflow.com/questions/10610131/checking-if-a-field-contains-a-string](https://stackoverflow.com/questions/10610131/checking-if-a-field-contains-a-string) – ambianBeing Sep 03 '19 at 16:49

1 Answers1

0

You need to make some changes in code. So use code below.

router.post('/search', ensureAuthenticated, async(req, res) => {
    const { name } = req.body;
    console.log(name);
    //I want to get the username and that specific data printed which is inside an array called books
    const Results = await User.find( { $text: { $search: name } } );
    console.log(Results);
    res.redirect('borrow');
});
Rahul kumar
  • 116
  • 7
  • UnhandledPromiseRejectionWarning: MongoError: text index required for $text query at Connection. (/Users/vaibhav2001/Documents/GitHub/Books4u/node_modules/mongodb-core/lib/connection/pool.js:443:61) at Connection.emit (events.js:198:13) at processMessage (/Users/vaibhav2001/Documents/GitHub/Books4u/node_modules/mongodb-core/lib/connection/connection.js:364:10) at TLSSocket. (/Users/vaibhav2001/Documents/GitHub/Books4u/node_modules/mongodb-core/lib/connection/connection.js:533:15) – Vaibhav Agrawal Sep 04 '19 at 10:57
  • You have created text index in model. Can you send your model – Rahul kumar Sep 04 '19 at 11:28
  • UserSchema.index({name: 'text' }); – Rahul kumar Sep 04 '19 at 11:29
  • const mongoose = require('mongoose'); const UserSchema = new mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true }, password: { type: String, required: true }, date: { type: Date, default: Date.now() }, books: { type: Array, required: false } }); const User = mongoose.model('User', UserSchema); module.exports = User; – Vaibhav Agrawal Sep 04 '19 at 14:38