0

for this query i am getting the result of all the documents where the sensor name id d

db.sensors.find( { "sensorData.sensorName" : { $regex : /d/i } } );

but when try usiing this query with nodejs route i am getting empty array back router.get('/api/sensorsDetails/isUnique/:sensorName',function(req,res){ console.log(req.params.sensorName)

SensorsModel.find({"sensorData.sensorName":{ $regex : /req.params.sensorName/i }},function (err,result) {

    console.log(result)
    if (err) {
        res.send(err.stack)
    }
    else {
        if(result.length>0){
            res.send(JSON.stringify({ status: false}))
        }else{
            res.send(JSON.stringify({ status: true}))
        }
    }
})

please help whats is wrong with second query

roberrrt-s
  • 7,914
  • 2
  • 46
  • 57
dhana lakshmi
  • 847
  • 1
  • 12
  • 29
  • 1
    See http://stackoverflow.com/questions/7101703/how-do-i-make-case-insensitive-queries-on-mongodb – Wiktor Stribiżew Oct 17 '16 at 10:15
  • i tried like this SensorsModel.find({"sensorData.sensorName":{ $regex : "/"+req.params.sensorName+"/i" }} but i am getting a empty result even thought there is a match – dhana lakshmi Oct 17 '16 at 12:31
  • 1
    Sure, you are passing a regex literal inside a C string literal, which is not correct. To pass a variable into a regex, use a RegExp constructor: `SensorsModel.find({"sensorData.sensorName":{ $regex : RegExp(req.params.sensorName, "i") }} ` – Wiktor Stribiżew Oct 17 '16 at 12:34

0 Answers0