Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式和通配符查询任意数据_Regex_Performance_Mongodb - Fatal编程技术网

Regex 使用正则表达式和通配符查询任意数据

Regex 使用正则表达式和通配符查询任意数据,regex,performance,mongodb,Regex,Performance,Mongodb,鉴于这种数据结构 { _id: ..., data: [ {key: 'articles.0.photos.0.category', value: '...'}, {key: 'articles.0.photos.1.category', value: '...'}, ... ] } 。。。我想按键和值进行查询,例如 key == 'articles.0.photos.0.category' && valu

鉴于这种数据结构

{
    _id: ...,
    data: [
        {key: 'articles.0.photos.0.category', value: '...'},
        {key: 'articles.0.photos.1.category', value: '...'},
        ...
    ]
}
。。。我想按键和值进行查询,例如

key == 'articles.0.photos.0.category' && value == 'something'
然而键和值是完全任意的,因此键值模式必须能够在这两个元素上创建索引。键中的数字表示数组中的索引

如果查询只查找逻辑意义,而不是数组中的位置,则需要按正则表达式进行搜索,例如

^articles\.\d+\.photos\.\d+\.caption

这是一个灾难性性能的公式,还是在更大的数据集中可以使用?是否有更好的解决方案来索引和查询任意数据?

在直接对db运行了一些测试之后,我得出结论,使用上述正则表达式在我的场景中非常有效。在包含100个键值对象的嵌套数组的200k文档样本集上,完整索引扫描平均从字符串匹配的0.001ms上升到正则表达式匹配的0.004ms。这就够了。不使用索引时,查询时间从5秒到20秒不等