5

In this question I'm referring to Database commands and JavaScript methods.

I wonder why MongoDB has 2 different sets of operations for commands and methods. First I though that commands are a subset of operations available in JavaScript, but then I realized that there are commands like filemd5 which are not [directly] available as methods.

My question is why there is a distinction between commands and methods and why they cannot simply be the same set of operations having different representations. The command one is more suitable for declarative operations (for example available via REST) and the second is appropriate for DB scripting.

1 Answer 1

10

JavaScript methods

The JavaScript methods can be seen as abstraction layer. Many methods are just wrappers for database commands. You can introspect the methods by just writing the function name without ().

Example:

> db.stats
function (scale) {
    return this.runCommand({dbstats:1, scale:scale});
}

Some methods execute multiple commands and aggregate the output. Example:

> db.printCollectionStats
function () {
    var mydb = this;
    this.getCollectionNames().forEach(function (z)      
        {print(z);printjson(mydb.getCollection(z).stats());print("---");});
}

JavaScript methods vs. Database commands

Some methods like find, update, delete etc. do not call commands:

> db.coll.find
function (query, fields, limit, skip) {
    return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip);
}

The MongoDB wire protocol specifies different request opcodes for query, insert, update, delete, getmore, killcursors etc.

Commands could have been implemented using different request opcodes. However, the MongoDB authors decided to implement commands as regular queries on a special collection called $cmd.

The introspection of runCommand reveals it:

> db.runCommand
function (obj) {
    if (typeof obj == "string") {
        var n = {};
        n[obj] = 1;
        obj = n;
    }
    return this.getCollection("$cmd").findOne(obj);
}

Internal commands

Some commands are internal and not intended to be called by the user. Hence, no method is offered in the MongoDB shell to conveniently access those commands.

A few examples:

3
  • Thanks for the answer. But it doesn't completely answer my question why all methods cannot be represented as commands or vice versa. For instance db.collection.insert uses a native call rather than command.
    – ejboy
    Mar 18, 2013 at 12:48
  • 2
    I would say that this is a design decision of the MongoDB authors. Inserts, updates, deletes, killCursors have their own opcode in the MongoDB wire protocol (docs.mongodb.org/meta-driver/latest/legacy/…). Commands, however, are just queries on the $cmd collection. Mar 18, 2013 at 13:57
  • I see. Although in some cases it would be so much easier if CRUD operations were exposed as commands as well. BTW thanks for a link to the wire protocol!
    – ejboy
    Mar 18, 2013 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.