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.

1 Answer 1

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);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! also it looks like i need a role that allows anyAction on anyResource to be able to run this. Struggling with that. Do you know how to do that? Can't find a good example :(
Sorry, never looked into that; probably best to post that as a separate question if you're stuck.
This is a great answer but eval is deprecated since version MongoDB 3.0, and they will remove it, I don't know what can be the alternative
.eval is deprecated from v4.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.