Reputation: 918
I am assuming that all commands that a user types into mongo shell are converted into a JSON string before being sent to the mongodb server for processing.
For example, if I send the following JSON to the mongodb server, the server will return the document where companyId equals "12345", here is that JSON:
{"companyId": "12345"}
Is there a way to have mongo shell display the actual command that is sent to the server for processing?
P.S. Just in case you are wondering about my assumption above, I am using the mongo-cxx driver to send commands to the mongodb server. All commands are formatted and sent to the mongodb server as JSON objects.
Upvotes: 0
Views: 321
Reputation: 65403
I am assuming that all commands that a user types into mongo shell are converted into a JSON string before being sent to the mongodb server for processing.
Your assumption about the raw commands that get sent to the server for processing is incorrect: the MongoDB wire protocol and server-side storage both use BSON (a binary JSON-like serialisation format). JSON is a convenient way to think about the data model (and work with data for human consumption and creation), but BSON is a superset of JSON data types and behaviour.
For example, BSON is an ordered binary data structure (not a dictionary) that does not require field names to be unique. BSON also supports additional data types such as binary data, 32-bit and 64-bit integers, floats, and Decimals (MongoDB 3.4+). MongoDB drivers take care of serialising data between different representations for convenience (eg language-specific data structures, JSON, or BSON) but if you think of MongoDB as a "BSON database" this may be useful to identify some performance improvements in your approach or explain some otherwise non-obvious behaviour like ordering of keys.
If you really want to see the raw conversation exchanged with a MongoDB deployment, you can use a network packet inspection tool like Wireshark that understands the MongoDB wire protocol. However, this is unlikely to be helpful unless you are trying to develop a new driver or debug a very low level server/driver issue.
If you are more interested in logging MongoDB queries or commands as received by the server, you can adjust the server log level to include appropriate detail (caveat: additional debug detail can easily be overwhelming) or consider using the MongoDB profiler. Increased logging or profiling levels should be used with caution in a busy production environment since these can add significant I/O and storage overhead.
Is there a way to have mongo shell display the actual command that is sent to the server for processing?
There isn't a built-in option to display the commands sent to the server, but you can perhaps add more debugging information by customising the mongo
shell with JavaScript.
The majority of common mongo
shell helpers are implemented as JavaScript functions that convert the provided arguments into a data structure that can be passed to a database command such as find
or aggregate
via db.runCommand()
. Data in the mongo
shell is converted from JavaScript to BSON for sending to a MongoDB deployment, and the server response is then converted from BSON to JavaScript for interpretation in the shell. The conversion between BSON and JavaScript is implemented by native C++ methods that are part of the mongo
shell source code.
If you want to see how the mongo
shell implements a specific helper to construct a command such as db.collection.insertOne()
, try running the command without parentheses: db.collection.insertOne
. Most of the shell helper logic will be about parameter validation and returning a result object but at some point the actual server communication will be delegated to a native C++ method which will show as [native code]
if you try to eval the function in the mongo
shell without parentheses.
I am using the mongo-cxx driver to send commands to the mongodb server
By default the C++ driver works directly with BSON data structures and avoids unnecessary serialisation to/from JSON using the document builder interfaces. However, JSON is a friendlier (but less performant) option if you want to manually create or view data.
For more context on BSON vs JSON, you may also be interested in my answer Why does MongoDB check for order of keys when matching embedded documents? on Software Engineering Stack Exchange.
Upvotes: 1