Running 'db.command(eval(commandString));' gets me 'MongoServerError: command not found'

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 triggers the query is executed correctly in the database, the document is updated, but the Mongo Server retreieves ‘MongoServerError: command not found’, I want to get rid of this error message and just have the query being executed successfully. Thanks in advance :smile:

@Jorge_Nava The error message “MongoServerError: command not found” indicates that the MongoDB server was unable to find the command that was executed. This could be due to a number of reasons, such as incorrect syntax or an invalid command. One possible reason is that the command being executed is not supported by the version of MongoDB that you are using.
To troubleshoot the issue, you can check the version of MongoDB that you are using and compare it to the documentation to ensure that the command is supported. You should also validate the syntax of the command and ensure that it is correct.
Another thing to consider is that the error message suggests that the command is being executed as a top-level command, rather than being executed in the context of a specific database. You may want to try running the command within a specific database by specifying the database name in the command.
Finally, you can try using a different approach to executing the command, such as using the MongoDB shell or a different driver method. For example, instead of using db.command(eval(commandString)), you could try using db.eval(commandString) or a different driver method that is better suited for the type of command that you are executing.
Overall, the key is to validate the syntax of the command, ensure that it is supported by the version of MongoDB that you are using, and consider using a different approach to executing the command if necessary.

1 Like