SamuraiJack
SamuraiJack

Reputation: 5549

What is the alternative to db.eval() for executing string MongoDb Query in nodejs in version 4.2 onwards?

I am looking for way to execute string mongodb queries such as :

 var q = 'db.collection("restaurants").find({   "address.coord": {     $lt: -95.754168   } })';
 this.db.eval('function(){ return ' + q + '.toArray(); }', function(err, res){
             console.log("the result is", res); 
 });o 

I understand from official MongoDb Documentation that this used to be possible earlier but it's not now.

Is there another way of achieving this?

I understand that I could store query condition and projections in variables and pass it on as dynamic entities. But is it also possible to go all the way dynamic and execute string queries fetched from a database on the fly?

Can I do without such dynamic nature? May be, may be not. But I want to know all my options.

Upvotes: 0

Views: 1409

Answers (1)

D. SM
D. SM

Reputation: 14520

But is it also possible to go all the way dynamic and execute string queries fetched from a database on the fly?

No.

The server is moving in the opposite direction and is expanding MQL and aggregation pipeline with additional (structured) functionality.

eval is meant to be replaced with the structured query options like the aggregation pipeline and $expr. Many of the aggregation pipeline stages and operators support variables.

If the aggregation pipeline isn't flexible enough, map/reduce continues to remain available as the closest direct alternative to eval.

Upvotes: 1

Related Questions