Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to use regex in $in operator in mongodb

Tags:

regex

mongodb

I am trying to use the regex with $in operator as mentioned in the docs but I am still unable to get the value.

db.users.find({'profile.firstName':{$in:["/justin/i"]}}).count() = 0

but when I used like this

db.users.find({'profile.firstName':{$in:["justin"]}}).count()=1 and

db.users.find({'profile.firstName':{$in:["Justin"]}}).count()=2

Edited Question

seems like I am not clear with the question I will add the code for easy understanding

I am trying to get the list of documents from a query which is case insensitive.I think it better to explain my doubt with code.

Meteor.users.find({'profile.firstName':{$in:[/justin/i,/helen/i]}}).count()

will give documents whose profile.firstName is justin/Justin/JUSTIn/HElen/helen

but my doubt how to give the variable x=["sai","test","jacob",---etc] in place of helen/justin

like image 954
Sai Ram Avatar asked Apr 29 '16 07:04

Sai Ram


People also ask

Can I use regex in MongoDB query?

MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support. In MongoDB, we can do pattern matching in two different ways: With $regex Operator. Without $regex Operator.

How do I use $in in MongoDB?

Use the $in Operator to Match Values This query selects all documents in the inventory collection where the value of the quantity field is either 5 or 15. Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

Where is MongoDB?

Description. The MongoDB $where operator is used to match documents that satisfy a JavaScript expression. A string containing a JavaScript expression or a JavaScript function can be pass using the $where operator. The JavaScript expression or function may be referred as this or obj.


2 Answers

You need to wrap the elements in the array with the RegExp object i.e.

regex = [new RegExp("sai", "i"), new RegExp("test", "i"),...]

You can use the map() method to map the elements in the array with the RegExp wrappers to a new array that you can then use in the regex query with $in:

var x = ["sai","test","jacob","justin"],
    regex = x.map(function (e) { return new RegExp(e, "i"); });

db.users.find({"profile.firstName": { "$in": regex } });

Using $in can be fairly efficient with small arrays but not so well with huge lists since it will skip around in the index to find the matching documents, or walk through the whole collection if there isn't an index to use.


Besides using the $in with the regular expression, you could use a pipe-delimited regex pattern with the keywords list like this:

var x = ["sai","test","jacob","justin"],
    regex = x.join("|");

db.users.find({
    "profile.firstName": {
        "$regex": regex, 
        "$options": "i"
    } 
}).count;
like image 174
chridam Avatar answered Sep 29 '22 16:09

chridam


try doing this way

db.users.find({'profile.firstName':{$in:[/justin/i]}}).count() = 0

you are passing regex as string.

Usually You should ask one problem at a time so question remains focused to a specific problem

well You can directly pass it, like this

// Here I am creating regex for all the elements in array
let myRegex = new RegExp(myArray.join('|'), i) 
and then I can pass this regex
db.users.find({'profile.firstName':{$in: myRegex}}).count() = 0
like image 36
Muhammad Faizan Avatar answered Sep 29 '22 16:09

Muhammad Faizan