문제

Hi i'm using mongodb and i=have few problems with mongodb regex

I have a collection with few fields and i want to search for a string in mongodb

when i enter this in my browser console,it is working fine and returning the count

posts.find({Headline: /google/i}).count()

it is returning the count which contains google as part of their headline

i tried this thing in my code and it is not returning count 0

var s_string="\/"+search_text+"\/";
var ss=Jobs.find({"J_Headline":search_text}).count();
console.log(ss);

whenever i give the full string of headline it is returning results

I don't know basics in regex help me with this.

Thx in advance

도움이 되었습니까?

해결책

The search needs to be a RegExp object. Try this:

var search = new RegExp(search_text, 'i');
var ss=Jobs.find({'J_Headline': search}).count();
console.log(ss);

다른 팁

I think you also should check very good solution http://matteodem.github.io/meteor-easy-search/, it also have elastic search support

This is how regex can be used with MongoDB

db.<collection>.find({<field>: {$regex: /<key./}}).pretty()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top