如何在Mongo中查询基于表达式的字符串

时间:2019-03-28 10:35:56

标签: regex mongodb

我在Mongo DB中有很多数据,我想基于String值进行查询,并且该值包含一个URL

"url" : "http://some-host/api/name/service/list/1234/xyz"

执行以下查询时,我得到了记录计数

db.mycollection.find({url:"http://some-host/api/name/service/list/1234/xyz"}).count()

我想获取所有与之匹配的记录

some-host/api/name/service/list/

我尝试使用下面的样品

db.mycollection.find({url:"some-host/api/name/service/list/"}).count() 

获得零记录

db.mycollection.find({url:/.*"some-host/api/name/service/list/".*/}).count()

错误

db.mycollection.find({"url":/.*"some-host/api/name/service/list/".*/}).count()

错误

db.mycollection.find({"url":/.*some-host/api/name/service/list/.*/}).count()

错误

db.mycollection.find({"url":/.*some-host//api//name//service//list//.*/}).count()

知道了...

...

然后没有回应

1 个答案:

答案 0 :(得分:1)

您尝试过这样的事情吗?

db.mycollection.find({'url': {'$regex': 'sometext'}})

也请检查here

相关问题