Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a mongo query string on a node server using native node driver?

I'd like to pass a string like "db.users.find()" to the node server and have it execute the command. This question: How to execute a MongoDB query in the native node-mongo-native driver? has an answer for the C- driver.

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

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

and it doesn't work. Appreciate the help.

like image 577
wemadeit Avatar asked Sep 19 '25 12:09

wemadeit


1 Answers

You're close, but the function you create needs to return something usable to the callback. For example:

var query = 'db.users.find()';
db.eval('function(){ return ' + query + '.toArray(); }', function(err, result){
  console.log("the result is", result);
});
like image 84
JohnnyHK Avatar answered Sep 21 '25 02:09

JohnnyHK