0

I want to check the user if exists but in case sensitive, For example= ABC is existing and if user type abc or Abc than it should say that name exists

This is my current logic

company_model.companySchema.pre('save', function (next) {
    try {
        cModel.find({ name: this.name }, function (err, docs) {
            try {
                if (!docs.length) {
                    next();
                } else {
                    console.log('company name exists: ', this.name);
                    next(new Error("Company Name exists!"));
                }
            } catch (e) {
                console.error('error in cModel.find: ' + e.message)
            }
        });
    } catch (e) {
        console.error('error in pre save : ' + e.message)
    }
});


rdarioduarte
  • 1,929
  • 2
  • 21
  • 29
  • Have you seen?? https://stackoverflow.com/questions/8993773/contains-case-insensitive – bakar_dev Oct 09 '19 at 07:48
  • @bakar_dev I am unable to struct that logic with my code, can you help me with that? –  Oct 09 '19 at 07:49
  • 1
    so you want case **in**sensitive – Bravo Oct 09 '19 at 07:54
  • @bakar_dev - that won't help since it's some database ... perhaps [this SO question](https://stackoverflow.com/questions/7101703/how-do-i-make-case-insensitive-queries-on-mongodb) is better - which is a duplicate of [this one](https://stackoverflow.com/questions/1863399/mongodb-is-it-possible-to-make-a-case-insensitive-query) - so, a simple search on SO would've found at least two duplicates – Bravo Oct 09 '19 at 07:56
  • @Bravo can you help me with code? editing it? –  Oct 09 '19 at 08:01
  • `cModel.find({ name: { $regex: new RegExp(\`^${this.name}$\` , 'i') } }, ....` – Bravo Oct 09 '19 at 08:04
  • You can make the data you get in your database lowercase and change the user input also into lowercase then compare it. – Vincent Dante Oct 09 '19 at 08:21
  • Possible duplicate of [MongoDB: Is it possible to make a case-insensitive query?](https://stackoverflow.com/questions/1863399/mongodb-is-it-possible-to-make-a-case-insensitive-query) – rdarioduarte Oct 09 '19 at 08:22

0 Answers0