简体   繁体   English

无法在mongodb中的$ in运算符中使用正则表达式

[英]unable to use regex in $in operator in 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. 我正在尝试使用文档中提到的$in运算符的regex ,但我仍然无法获得该值。

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()=1

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 将提供其profile.firstNamejustin/Justin/JUSTIn/HElen/helen

but my doubt how to give the variable x=["sai","test","jacob",---etc] in place of helen/justin 但我怀疑如何给变量x=["sai","test","jacob",---etc]代替helen/justin

You need to wrap the elements in the array with the RegExp object ie 您需要使用RegExp对象包装数组中的元素,即

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 : 您可以使用map()方法将数组中的元素与RegExp包装器映射到一个新数组,然后您可以在带有$in的正则表达式查询中使用$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. 使用$in对于小型数组可能相当有效,但对于大型列表则不太好,因为它将在索引中跳过以查找匹配的文档,或者如果没有要使用的索引则遍历整个集合。


Besides using the $in with the regular expression , you could use a pipe-delimited regex pattern with the keywords list like this: 除了在正则表达式中使用$ in之外,您还可以使用管道分隔的正则表达式模式和关键字列表,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM