node.js 如何在 MongoDB 查询中使用正则表达式变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26699885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a regex variable in a query for MongoDB
提问by Tinniam V. Ganesh
I would like to make query MongoDB for documents based on a regex expression that I contruct. For e.g I have constructed a simple regex as follows that is a combination of a random letter and a random number for in Nodejs
我想根据我构造的正则表达式查询 MongoDB 的文档。例如,我构建了一个简单的正则表达式,如下所示,它是 Nodejs 中随机字母和随机数的组合
var randnum = Math.floor((Math.random() * 10) + 1);
var alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z'];
var randletter = alpha[Math.floor(Math.random() * alpha.length)];
var val = randletter + randnum + '.*;
I have tried various combinations for the regex variable like
我已经尝试了正则表达式变量的各种组合,例如
var stream = collection.find({"FirstName": /val/).stream();
&&
var stream = collection.find({"FirstName": /$val/).stream();
&&
var stream = collection.find({"FirstName": {$in : [{$regex:val}]}}).stream()
&&
var stream = collection.find({"FirstName": {$in : [{$regex:$val}]}}).stream()
None o it seem to work. However when I write the actual regex I get the records for e.g.
没有 o 它似乎有效。但是,当我编写实际的正则表达式时,我得到了例如的记录
var stream = collection.find({"FirstName": /J.*/).stream();
Any help will be appreciated.
任何帮助将不胜感激。
Thanks Ganesh
谢谢 Ganesh
回答by JohnnyHK
You need to create a regular expression object from the string using the RegExpconstructor as the /.../syntax is only for use with literals.
您需要使用RegExp构造函数从字符串创建正则表达式对象,因为/.../语法仅用于文字。
var stream = collection.find({"FirstName": new RegExp(val)}).stream();
回答by Hamza Khan
I was having same problem with my title and after lots of searching, i found this answer Here,
我的标题遇到了同样的问题,经过大量搜索,我在这里找到了这个答案,
var search = 'Joe';
db.users.find(name: /^search/)
db.users.find(name: {$regex: /^search/});
db.users.find(name: {$regex: "/^" + search + "/"});
The queries above won't return anything. The solution to this little problem is quite simple:
上面的查询不会返回任何内容。这个小问题的解决方法很简单:
db.users.find(name: new RegExp(search)) //For substring search, case sensitive.
db.users.find(name: new RegExp('^' + search + '$')) //For exact search, case sensitive
db.users.find(name: new RegExp(search, ‘i')) //For substring search, case insensitive
db.users.find(name: new RegExp('^' +search + '$', 'i')); //For exact search, case insensitive
Other flags or properties can be added base on reference here
可以根据此处的参考添加其他标志或属性
回答by Wizard
If you want to use the variable valas a parameter of the query part, you can use $regex, for example:
如果要使用变量val作为查询部分的参数,可以使用$regex,例如:
collection.find({"FirstName": {$regex:val}})
It is not allowed to put $regexin $inoperator according to the manual.
根据手册,不允许将$regex放在$in运算符中。
If you want to put regular expression object in $inoperator, you have to use JavaScript regular expression object, for example:
如果要将正则表达式对象放在$in运算符中,则必须使用 JavaScript 正则表达式对象,例如:
collection.find({"FirstName": {$in: [/abc/, /123/]}})
By the way, valin /val/is constant string, not the variable as you defined above.
顺便说一下,valin/val/是常量字符串,而不是你上面定义的变量。
回答by Murtaza Manasawala
Use the following code to use dynamic value in regex with or operations
使用以下代码在正则表达式或操作中使用动态值
{$match: { $or: [{ 'title': { $regex: request.query.val, $options: 'i'} }, { 'skills_required': { $regex: request.query.val, $options: 'i'} }] }},
回答by Pedro JR
let test = `what your regex should
search`;
collection.find({
fieldName: {$regex: test}
}).then(result => red.send(result));
// tested on mongoose but should work on mongodb too
// 在 mongoose 上测试过,但也应该在 mongodb 上工作

