Question

I have tried to search around for clues but didn't find any. Please let me ask a question about reverse search for mongodb.

I have a collection that contain a phone prefix, let say bellow.

{"prefix":"1234"}

and i am trying to make it hit with value like 12341111 or 12342222 ... in MySQL i can do it with below SQL.

SELECT * from phoneprefix where ‘12341111’ like concat(prefix,’%’)

Is there anyway to make it possible at mongodb ?

Thanks for reading my post and please have a nice day !

Was it helpful?

Solution

The only way I can see to do this is to use the performance-challenged $where operator:

db.phoneprefix.find({$where: function() {
    return (new RegExp('^' + this.prefix)).test('12341111'); 
}});

OTHER TIPS

You could use $regex in MongoDB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top