I'm using mongoDB Atlas to store some data. The collection is like,
var User = new Schema({
firstname: {
type: String,
default: ''
},
lastname: {
type: String,
default: ''
}
}, {
timestamps: true
});
But I'm using Passport for authentication, username and password is handled by it.
When searching for users by username, I want to display a list of usernames with matching characters. Like, auto complete suggestions.
I realize I have to use regex in some way to achieve this, but I don't know how! For now, I'm doing this,
router.post('/search', corsWithOptions, (req, res, next) => {
User.findByUsername(req.body.username).then(user => {
console.log(user);
res.json(user)
})
})
But until the entire username is available, this method returns null.
Here, findByUsername is a method in Passport, takes 2 params (username, selectHashSaltFields).
I also tried this,
User.find({
$or: [
{
username: { '$regex': '.*' + req.body.search, $options: 'i' },
firstname: { '$regex': '.*' + req.body.search, $options: 'i' },
lastname: { '$regex': '.*' + req.body.search, $options: 'i' }
}
]
}).then(user => {
console.log(user)
})
This is returning an empty list, even when the values exist
This is a MERN stack, how to query documents and show suggestions for auto complete in frontend?