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
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.
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.
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.
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With