-1

I have a JS application where I have a mongodb driver. After connecting to it I try to run

const result = await db.command(eval(commandString));

in order to run diverse types of commands into my database from a string, for example:

db.collection(“users”).updateOne(
  {
    name: “Rodrigo” 
  }, 
  { 
    $set: { 
      age: 24 
    } 
  }
)

When I trigger the query is executed correctly in the database, and the document is updated, but the Mongo Server receives

MongoServerError: command not found

I want to get rid of this error message and just have the query executed successfully.

I've tried running different commands but all of them have the same behavior.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
eizo9
  • 1
  • 3

1 Answers1

1

db.command() method is unable to find command you are trying to execute because it isn't a valid MongoDB command. FYI, eval() method in MongoDB is deprecated.

Starting in version 4.2, MongoDB removes the eval command. The deprecated db.eval(), which wraps the eval command, can only be run against MongoDB 4.0 or earlier versions. For behavior and example, refer to the 4.0 or earlier version of the manual. https://www.mongodb.com/docs/v4.2/reference/method/db.eval/

you should use the appropriate methods provided by the MongoDB driver for Node.js :

const collection = db.collection('users');
const filter = { name: 'Rodrigo' };
const update = { $set: { age: 24 } };
const result = await collection.updateOne(filter, update);
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68