2016-08-04 104 views
0

我想匹配所有文件,其中Url字段在db中包含shouldmatch,以任意顺序。mongo正则表达式查询中的多个条件

在例如那些应该匹配:

http://www.myurl.com/should/match

http://www.myurl.com/match/should

但不是http://www.myurl.com/no/match

我试了正则表达式,但不匹配。 I.e:

db.mycollection.find({"Url":/^(?=.*\should\b)(?=.*\match\b).*$/}) 

返回无匹配。

感谢任何帮助。 问候

+0

会一直跟随另一个或可能像'应该/你/匹配'这样的差距? – depperm

+0

@depperm可能会出现问题。它们应该存在于任何字符串中。 – user1665355

+1

我现在无法测试这个,但是“Url”:/.*应该是\ /(。+ \ /)?匹配|。*匹配\ /(。+ \ /)吗?\ – depperm

回答

0

设置的那些话开始边界与\b你的正则表达式,你已经设定结束边界

db.mycollection.find({ "Url": /^(?=.*\bshould\b)(?=.*\bmatch\b).*$/ }) 

在文件

{ "Url" : "http://www.myurl.com/should/match" } 
{ "Url" : "http://www.myurl.com/match/should" } 
{ "Url" : "http://www.myurl.com/no/match" } 

集合返回

{ "Url" : "http://www.myurl.com/should/match" } 
{ "Url" : "http://www.myurl.com/match/should" } 
+0

不工作以及 – user1665355

+0

再次检查,我用'url'而不是'Url' – tarashypka

+0

仍然不工作伙伴 – user1665355