Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 /*N.*/和/N/(在MongoDB的正则表达式中)之间有区别吗_Regex_Mongodb - Fatal编程技术网

Regex /*N.*/和/N/(在MongoDB的正则表达式中)之间有区别吗

Regex /*N.*/和/N/(在MongoDB的正则表达式中)之间有区别吗,regex,mongodb,Regex,Mongodb,我想在我的MongoDB中找到名称字段中包含大写字母N的文档。以下两次搜索都返回正确的结果 db.col.find({name: /N/}) db.col.find({name: /.*N.*/}) 到目前为止,我一直在第一个find()-语句中使用正则表达式,因为我相信如果我不使用^或$的话,*是被添加的。但是我从来都不知道是否有区别。第一个比第二个快。 以下是Perl中的基准测试: #!/usr/bin/perl use strict; use warnings; use Benchma

我想在我的MongoDB中找到名称字段中包含大写字母N的文档。以下两次搜索都返回正确的结果

db.col.find({name: /N/})
db.col.find({name: /.*N.*/})

到目前为止,我一直在第一个
find()
-语句中使用正则表达式,因为我相信如果我不使用
^
$
的话,
*
是被添加的。但是我从来都不知道是否有区别。

第一个比第二个快。
以下是Perl中的基准测试:

#!/usr/bin/perl 
use strict;
use warnings;
use Benchmark qw(:all);

my @data = (
    'jkhfq lqksjdfh N lmkjqf N kjlh',
    'NNNNNNNNNN',
    'lkjh vuzyd vuyr gdqhgsjkhdg',
);
my $count = -2;
cmpthese($count, {
    'anchor' => sub {
        my @match = grep{/^.*N.*$/} @data;;
    },
    'simple' => sub {
        my @match = grep { /N/ } @data;
    },
});
结果:

           Rate anchor simple
anchor 378332/s     --   -28%
simple 521907/s    38%     --

regex
/N/
/^.*N.*$/

这在MongoDB的regexp引擎中也应该适用-第一个regexp的运行速度比第二个快。我对其他regexp进行了排名:
/N/
/^.+N.*+$/
/.*N.*/
/./.*N.*/
/.*././.*./.
<
/^.*N.*?$/
/.+N.*+/