Why `distinct` and `count` operations generate `“op”: “command”` in system.profile

dba.stackexchange https://dba.stackexchange.com/questions/176631

  •  07-10-2020
  •  | 
  •  

Question

According to documentation count and distinct queries should result in op = count and op = distinct in system.profile. However when I tried to run such queries in shell both were generated as op = command.

> db.p.count()
23
> db.p.distinct("item")
[ "pencil", "eraser" ]
> db.system.profile.find().sort({$natural: -1}).limit(2).pretty()
{
    "op" : "command",
    "ns" : "test.p",
    "command" : {
        "distinct" : "p",
        "key" : "item",
        "query" : {

        }
    },
    (...)
}
{
    "op" : "command",
    "ns" : "test.p",
    "command" : {
        "count" : "p",
        "query" : {

        },
        "fields" : {

        }
    },
    (...)
}

For remove this works perfectly fine, I execute remove and I get op = remove in system.profile.

> db.p.remove({item: "pen"})
WriteResult({ "nRemoved" : 0 })
> db.system.profile.find().sort({$natural: -1}).limit(1).pretty()
{
    "op" : "remove",
    "ns" : "test.p",
    "query" : {
        "item" : "pen"
    },
    (...)
}
Was it helpful?

Solution

Originally, all database operation in MongoDB are encoded in the Wire Protocol. However, this would mean that creating a new command would involve:

  1. Creating a new opcode in the wire protocol, and
  2. Bumping the wire protocol version.

Another disadvantage with this approach is that the format of the opcodes cannot change without changing the wire protocol version. As more features are introduced, this situation will become unavoidable and potentially occur more often in the future.

To overcome this limitation, newer version of MongoDB started to migrate the existing wire protocol functions into its command form. For example, the command forms of the CRUD operations are detailed in the Query and Write Operation Commands documentation page.

This is the main reason why some operations are recorded as commands, and some are recorded as the actual wire protocol codes. Eventually, the command forms will be the preferred form in the future.

There is no single generic ticket that describes this move, but the DOCS-7489 ticket kind of describes the process for the drivers.

For a more in-depth technical details on upcoming driver changes with regard to the wire protocol, please see New Driver Features for MongoDB 3.6, specifically the section about OP_MSG.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top