On this page

Runs the specified SQL statement and returns the result as a cursor.


db.runCommand({sql: <string>})


The following example runs an OPTIMIZE TABLE command, where dbTest and mycollection are the names of the database and collection, respectively.

db.runCommand({ sql: "OPTIMIZE TABLE `dbTest`.`mycollection` FULL" });

Here's another example that runs a SELECT statement:

db.runCommand({ sql: "SELECT * FROM `dbTest`.`mycollection`" });

Last modified: May 15, 2023

Was this article helpful?