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.