Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 679 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make case-insensitive queries on Mongodb?

#11
1. With Mongoose (and Node), this worked:

- `User.find({ email: /^[email protected]$/i })`

- `User.find({ email: new RegExp(`\`^${emailVariable}$\`, 'i') })

2. In MongoDB, this worked:

- `db.users.find({ email: { $regex: /^[email protected]$/i }})`

Both lines are case-insensitive. The email in the DB could be `[email protected]` and both lines will still find the object in the DB.

Likewise, we could use `/^[email protected]$/i` and it would still find email: `[email protected]` in the DB.
Reply

#12
**... with mongoose on NodeJS that query:**

const countryName = req.params.country;

{ 'country': new RegExp(`^${countryName}$`, 'i') };

or

const countryName = req.params.country;

{ 'country': { $regex: new RegExp(`^${countryName}$`), $options: 'i' } };

// ^australia$

or

const countryName = req.params.country;

{ 'country': { $regex: new RegExp(`^${countryName}$`, 'i') } };

// ^turkey$


**A full code example in Javascript, NodeJS with Mongoose ORM on MongoDB**

// get all customers that given country name
app.get('/customers/country/:countryName', (req, res) => {
//res.send(`Got a GET request at /customer/country/${req.params.countryName}`);

const countryName = req.params.countryName;

// using Regular Expression (case intensitive and equal): ^australia$

// const query = { 'country': new RegExp(`^${countryName}$`, 'i') };
// const query = { 'country': { $regex: new RegExp(`^${countryName}$`, 'i') } };
const query = { 'country': { $regex: new RegExp(`^${countryName}$`), $options: 'i' } };

Customer.find(query).sort({ name: 'asc' })
.then(customers => {
res.json(customers);
})
.catch(error => {
// error..
res.send(error.message);
});
});
Reply

#13
This will work perfectly\
```db.collection.find({ song_Name: { '$regex': searchParam, $options: 'i' } })```

Just have to add in your regex ```$options: 'i'``` where **i** is case-insensitive.
Reply

#14
Regex queries will be slower than index based queries.

You can create an index with specific collation as below \
`db.collection.createIndex({field:1},{collation: {locale:'en',strength:2}},{background : true});`

The above query will create an index that ignores the case of the string. The collation needs to be specified with each query so it uses the case insensitive index.

Query\
`db.collection.find({field:'value'}).collation({locale:'en',strength:2});`

*Note* - if you don't specify the collation with each query, query will not use the new index.

Refer to the mongodb doc here for more info -

[To see links please register here]

Reply

#15
I have solved it like this.

var thename = 'Andrew';
db.collection.find({'name': {'$regex': thename,$options:'i'}});
If you want to query for case-insensitive and exact, then you can go like this.

var thename = '^Andrew$';
db.collection.find({'name': {'$regex': thename,$options:'i'}});
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through