How would I search for queries in MongoDB (using the mongoose library) without them being case sensitive?
Lets say I have a query that has the string data of Adam
. That query is case sensitive, so searching for that query as adam
will return a null.
I tried using regex, and disabling the case sensitivity with it. It did the job, but now whenever I search for ad
it finds & returns Adam
, which is something that I don't want.
How would I search for case insensitive queries the right way? Thank you!
Here's my code for finding queries without case sensitivity, but with the regex bug.
server.get('/test/:username', (req, res)=> {
const username = req.params.username
usersDB.findOne({'username': {'$regex': username, $options: '?-i'}}, (err, data)=> {
if(err) {
throw err
}
if(!data) {
console.log('user not found')
res.redirect('/')
} else if(data) {
console.log('user found: ' + data.username)
res.redirect('/')
}
})
})