joshkurz
joshkurz

Reputation: 1033

advanced query mongodb

I am trying to query a table in mongo by using multiple keys. I have been able to use the $in function to pull results utilizing the cmd.

I have also been successful at making mongojs play nice with node and query the database when I inject the json object that represents the query into the find() method. Here is that code that works.

db.collection(req.params.collection).find({"labels":{"$in":["key1","key2"]}}) //json injected

When I try to inject the json object into the find() method directly I get zero results.

{"labels":{"$in":["key1","key2"]}} is the exact json object created by the server and used by the method above.

var json = JSON.stringify(eval("(" + newQ + ")"));
console.log(json);
db.collection(req.params.collection).find(json)  //same json object as above. 

Im lost on this one. I need to be able to dynamically set the json object and insert into the find method.

Upvotes: 1

Views: 346

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33155

You don't need to convert your JSON to a string before sending it to mongodb. The find command accepts native JSON as its query parameter.

Are you using the node native driver?

Upvotes: 2

Related Questions