如何查询mongodb中存储的正则表达式

如何查询mongodb中存储的正则表达式,mongodb,Mongodb,文档使用regex存储,如 { "id": "id-1", "blocked": [ "abcd.com/[a-z]+", "efgh.com/[0-9]+" ] }, { "id": "id-2", "blocked": [ "pqrs.com/[\w]+", &q

文档使用regex存储,如

{
  "id": "id-1",
  "blocked": [
    "abcd.com/[a-z]+",
    "efgh.com/[0-9]+"
  ]
},
{
  "id": "id-2",
  "blocked": [
    "pqrs.com/[\w]+",
    "xyz.com/[.]+"
  ]
}
基本上,我正在研究如何查询存储的正则表达式

案例1:如果我通过了“abcd.com/mongodb”,那么它将与第一个文档匹配。所以查询应该重新运行第一个文档


案例2:如果我通过了“google.com/mongo”,那么它与任何存储的正则表达式都不匹配。所以查询应该返回空数组。

我不确定我是否完全理解您的问题,但这里有一个快速尝试来解决我认为我得到的问题

请随意评论。它没有经过优化:每次迭代都创建新的RegExp可能不是最好的主意,只是实现起来更简单

const文档=[{
“id”:“id-1”,
“阻止”:[
“abcd.com/[a-z]+”,
“efgh.com/[0-9]+”
]
},
{
“id”:“id-2”,
“阻止”:[
“pqrs.com/[\w]+”,
“xyz.com/[.]+”
]
}];
常量输入=[“abcd.com/mongodb”,“google.com/mongo”];
功能检查(输入){
documents.forEach(document=>{
const matchFound=document.blocked.some(regexp=>newregexp(regexp.test)(输入));
如果(找到匹配项){
log(`Input'${Input}匹配的文档${document.id}`);
}
});
}

输入。forEach(检查)这是否回答了您的问题?它可能不起作用,如果我将输入转换为regex,
\abcd.com\mongodb\$
,那么它将与文本
abcd.com/[a-z]+
不匹配。你是说另一种方式
abcd.com/[a-z]+
\abcd.com\mongodb\$
不匹配?也许你的regexp是错的。。。我真的不明白你这里的问题。如果要将字符串转换为正则表达式,请使用构造函数
new RegExp()
,它将完成此任务。对吗?@sjahan谢谢你的支持。通常我们在文档中存储
文本
,并使用将提供的
$regex
进行查询。但这种情况有点不同。在这里,我们存储了一些正则表达式,我将提供
文本。现在我想返回满足匹配条件的文档。它就像反向正则表达式。
//MongoDB version on windows(actual code output from mongo shell 4.2 on windows)
> db.version();
4.2.6
//prepare the same data and query it as below, per problem statement
> db.test5.find()
{ "_id" : ObjectId("5f721b8a3d04e9033d482a9b"), "id" : "id-1", "blocked" : [ "abcd.com/[a-z]+", "efgh.com/[0-9]+" ] }
{ "_id" : ObjectId("5f721b8a3d04e9033d482a9c"), "id" : "id-2", "blocked" : [ "pqrs.com/[w]+", "xyz.com/[.]+" ] }
declare variables to include the string for which regex can be checked from collection
> var1 = "abcd.com/mongodb";
abcd.com/mongodb
> var2 = "google.com/mongo";
google.com/mongo
//actual code output from mongo shell using aggregate
//you need to use $regexMatch to match the variable string to that is stored in the collection
> db.test5.aggregate([
... {$unwind:"$blocked"},
... {$addFields:{result1:{$regexMatch:{input:var1,regex:"$blocked"}}}},
... {$addFields:{result2:{$regexMatch:{input:var2,regex:"$blocked"}}}}
... ]);
{ "_id" : ObjectId("5f721b8a3d04e9033d482a9b"), "id" : "id-1", "blocked" : "abcd.com/[a-z]+", "result1" : true, "result2" : false }
{ "_id" : ObjectId("5f721b8a3d04e9033d482a9b"), "id" : "id-1", "blocked" : "efgh.com/[0-9]+", "result1" : false, "result2" : false }
{ "_id" : ObjectId("5f721b8a3d04e9033d482a9c"), "id" : "id-2", "blocked" : "pqrs.com/[w]+", "result1" : false, "result2" : false }
{ "_id" : ObjectId("5f721b8a3d04e9033d482a9c"), "id" : "id-2", "blocked" : "xyz.com/[.]+", "result1" : false, "result2" : false }
>
//the result will be true or false boolean.
//Hope, you can use this result to process your further logic of return array or empty string
//eg. case1 per your requirements: Since var1 matches with string it matches to true
//similarly case2 per your requirements: Since var2 does not match it returns false