Home > Software engineering >  Case insensitive queries using mongodb and javascript
Case insensitive queries using mongodb and javascript

Time:10-07

I have the following mongodb code that works in returning a group name (regardless of case)

db.getCollection('groups').findOne({
      name: { $regex: /group1/i },
    });

However, i am trying to put this into my javascript app and i can't seem to get it to work with a variable storing the group name instead.

code = "group1";
const group: any = await this.groupModel.findOne({
  name: { $regex: /code/i },
});

Do i need special quotes around the code variable? Or does it need to be a different type or something?

CodePudding user response:

Figured it out, i had to change the regex line to this:

name: { $regex: code, $options: "i" },
  • Related