node.js - Mongodb Query does not work with string -


i trying documents in database match string not work when pass in variable.

i have string servicestring , servicestring = "test1", "test2", "test3"

        query = db.collection('services').find({             'service': {                 $in: [servicestring]             }         }); 

this returns nothing db if this:

        query = db.collection('services').find({             'service': {                 $in: ["test1", "test2", "test3"]             }         }); 

it works , returns need.

do know why not working, thinking string putting commas in string. whats way can because string input user can change cant hard code variables in query?

$in looking array.

so, it's better create array of string want find.

let servicestring = ["test1", "test2", "test3"];

note : can use var instead of let here

then query likes :

let servicestring = ["test1", "test2", "test3"]; query = db.collection('services').find({        'service': {            $in: servicestring       } }); 

more information : https://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#special-query-operators


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -