Like Query with Meteor MongoDB

how to search products with like ( like SQL) ?

i tried with this query:

const search = ‘5000’;
const selector = {
’$or’: [
{ ‘name’: {’$regex’: search, ‘$options’: ‘x’} },
{ ‘price’: {’$regex’: search, ‘$options’: ‘x’} },
]
};
but this only work with string column, it not work with number.
i want to search all colums and all type? how to do that ?

Look into the $where operator:

{$or: [
  {$where: '/5000.*/.test(this.name)'}, 
  {$where: '/5000.*/.test(this.price)'}
]}
1 Like

i don’t know how, but it work :smiley: thanks so much @hwillson

FYI this is pretty inefficient since it needs to scan the whole database.

Yes - it also can’t take advantage of indexes. See the linked to docs for more info (@sashko - I think you meant it needs to do a full collection scan, not a full database scan?).

Yes that’s true. I meant full collection, sorry!

1 Like

Also important is sanitizing input to $where. If user input is passed in there it could easily dos your mongodb. $where executes arbitrary javascript and selects all records where that javascript evaluates to true.