2014-02-12 36 views
0

进出口使用快递代码MongoDB的搜索与字之间的间距

> var criteria = options.searchId; 

>  criteria = '.*' + criteria + '.*'; 

> this.find({ $or: [ { 'content' : { $regex : criteria , $options : 
> 'i' }} , { 'title' : { $regex : criteria , $options : 'i' }} ]}) 

搜索蒙戈集合中的字符串,但是当我搜索test test即时消息没有得到任何结果,但同样存在于内容栏(我觉得值是测试%20test)test即时得到正确的结果。我的问题是一个声明没有得到搜查,但一个单词在搜索。

+2

有点难理解你的问题,你能澄清还是详细说明? – Rishi

回答

1

它为我的作品:

> db.articles.drop() 
false 
> db.articles.insert({"title": "first", "content": "foo bar"}) 
> db.articles.insert({"title": "second", "content": "a test test b"}) 
> db.articles.insert({"title": "third", "content": "only test"}) 
> db.articles.insert({"title": "test test in title", "content": "lorem ipsum"}) 
> var query = {"$regex": ".*test test.*", "$options": "i"} 
> db.articles.find({"$or": [{"content": query}, {"title": query}]}) 
{ "_id" : ObjectId("52fb789ff00e23295fe08d86"), "title" : "second", "content" : "a test test b" } 
{ "_id" : ObjectId("52fb78f5f00e23295fe08d88"), "title" : "test test in title", "content" : "lorem ipsum" } 

这是你期待什么?

如果你有test%20test而不是test test那么这可能是你的web框架中的URL参数处理问题。