4

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.

Community
  • 1
  • 1
wemadeit
  • 507
  • 6
  • 12

1 Answers1

5

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);
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471