How do I execute a mongo query string on a node server using the native node driver?

I would like to pass a string like "db.users.find ()" to the node server and execute the command. See this question: How do I execute a MongoDB query in a native node-mongo-native driver? there is an answer for the C driver.

Is there a way to do this directly with the native node driver? I tried doing

db.eval('function(){'+query+'}', function(err, result){
  console.log("the result is", result
});

      

and it doesn't work. Appreciate help.

+3


source to share


1 answer


You are close, but the function you create should return something useful for the callback. For example:



var query = 'db.users.find()';
db.eval('function(){ return ' + query + '.toArray(); }', function(err, result){
  console.log("the result is", result);
});

      

+4


source







All Articles