Question

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

Was it helpful?

Solution

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);

OTHER TIPS

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top