Making a case-insensitive “like” query using the mongodb driver in Node

312
December 30, 2016, at 11:59 AM

I'm looking to make a query like mentioned in the title with an array of data.

I have this line working..

collection.find({"name": {"$in":[/papa JOhn's/i,"Taco Bell"]}}).toArray(function(err, items) {

However, the data is going to be dynamic and fed into the query as an array

restaurant_data = [rest1, rest2 .... restn]

I want to essentially have something like

collection.find({"name": {"$in": /restaurant_data/i }}).toArray(function(err, items) {

My Attempt..

let test_data = ['papa john', 'taco bell']
//Get an array of the restaurant names here
collection.find({"name": {"$in": test_data.map(element => {
  /element/i
})}}).toArray(function(err, items) {

My working attempt!

I got it doing this... Not sure if it's the most efficient

let test_data = ['papa john', 'taco bell']
collection.find({"name": {"$in": test_data.map((element) => {
  var regex = new RegExp(".*" + element + ".*");
  return regex
})}}).toArray(function(err, items) {
Answer 1
collection.find({"name": {"$in":
  restaurant_data.map(e => new RegExp(e, 'i'))
}}).toArray

has better chance to work. Note that /bla/i will match blabla so

collection.find({"name": {"$in": [/bla/i] } })

will match documents with name blabla Not sure this is what you want or not.

Rent Charter Buses Company
READ ALSO
Express serve.static() serving blank index.html for Angular app

Express serve.static() serving blank index.html for Angular app

I've come across a strange issue with Express servestatic where it works when I serve from my '/dist' directory but not from my '/src' directory

475
Interfacing C function from node.js using ffi

Interfacing C function from node.js using ffi

I am trying to call a C function in a library xxxlibso from node

696
cannot find module '../app/models/user'

cannot find module '../app/models/user'

im struggled with this problem like more then 40 hours and sill don't know how to solve this, the problem is a little big to explain but i will try my best, im a little newbie with NodeJs and mongo stuffs so forgive me any stupid mistake

463
Get the domain that requested a route in Express

Get the domain that requested a route in Express

If I have this client-side application running on port 3000 and it request something in my API, built on top of ExpressJS, running on port 8080, how do I find out that the request was originated on localhost:3000?

427