Search code examples
mongodblibmongoc

Running shell methods via libmongoc?


I'm trying to figure out if it's possible to run shell methods via libmongoc. I can run database commands by using the method mongoc_client_command. So for example, I can run listDatabases through mongoc_client_command, but I cannot run db.adminCommand( { listDatabases: 1 } ).

  1. Is it possible to run shell methods via the libmongoc library?
  2. If not, is there and alternative api to run shell commands on Mongodb?

Solution

  • mongo shell commands are just convenience wrappers for the underlying database command API.

    If you invoke most shell helpers without parentheses you can see what commands are being run. For example, db.adminCommand runs a command against the admin database:

    > db.adminCommand
    function (obj, extra) {
        if (this._name == "admin")
            return this.runCommand(obj, extra);
        return this.getSiblingDB("admin").runCommand(obj, extra);
    }
    

    Through the libmongoc interface you would pass admin as the db_name parameter to run a command with an equivalent outcome to db.adminCommand().