mongo shell - 显示发送到服务器的原始命令?
mongo shell - display raw command sent to server?
我假设用户输入 mongo shell 的所有命令在发送到 mongodb 服务器进行处理之前都被转换为 JSON 字符串.
例如,如果我将以下 JSON 发送到 mongodb 服务器,服务器将 return companyId 等于“12345”,这里是 JSON:
{"companyId": "12345"}
有没有办法让mongoshell显示发送到服务器进行处理的实际命令?
P.S。以防万一你对我上面的假设感到疑惑,我正在使用 mongo-cxx 驱动程序将命令发送到 mongodb 服务器。所有命令都被格式化并作为 JSON 个对象发送到 mongodb 服务器。
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.
您关于发送到服务器进行处理的原始命令的假设是不正确的:MongoDB wire protocol and server-side storage both use BSON(二进制 JSON 类序列化格式)。 JSON 是一种思考数据模型(以及处理供人类消费和创建的数据)的便捷方式,但 BSON 是 JSON 数据类型和行为的超集。
比如BSON是一种有序的二进制数据结构(不是字典),不需要字段名是唯一的。 BSON 还支持 additional data types 例如二进制数据、32 位和 64 位整数、浮点数和小数 (MongoDB 3.4+)。 MongoDB 驱动程序负责在不同表示之间序列化数据以方便使用(例如特定于语言的数据结构,JSON 或 BSON),但如果您将 MongoDB 视为 "BSON database"这可能有助于确定您的方法中的一些性能改进或解释一些其他不明显的行为,例如键的排序。
如果您真的想查看与 MongoDB 部署交换的原始对话,您可以使用像 Wireshark 这样理解 MongoDB 有线协议的网络数据包检查工具。但是,这不太可能有帮助,除非您正在尝试开发新的驱动程序或调试非常低级别的 server/driver 问题。
如果您对记录服务器收到的 MongoDB 查询或命令更感兴趣,您可以 adjust the server log level to include appropriate detail (caveat: additional debug detail can easily be overwhelming) or consider using the MongoDB profiler。在繁忙的生产环境中应谨慎使用增加的日志记录或分析级别,因为它们会增加显着的 I/O 和存储开销。
Is there a way to have mongo shell display the actual command that is sent to the server for processing?
没有显示发送到服务器的命令的内置选项,但您可以通过自定义 mongo
shell 和 JavaScript 来添加更多调试信息.
大多数常见的 mongo
shell 帮助器都是作为 JavaScript 函数实现的,这些函数将提供的参数转换为可以传递给 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 的数据结构。
如果你想看看 mongo
shell 如何实现一个特定的帮助程序来构造一个命令,例如 db.collection.insertOne()
,试试 运行 不带括号的命令:db.collection.insertOne
。大多数 shell 辅助逻辑将与参数验证和返回结果对象有关,但在某些时候,实际的服务器通信将委托给本机 C++ 方法,如果您尝试 eval,它将显示为 [native code]
mongo
shell 中的函数不带括号。
I am using the mongo-cxx driver to send commands to the mongodb server
默认情况下,C++ 驱动程序直接使用 BSON 数据结构并避免使用 document builder interfaces 进行不必要的序列化 to/from JSON。但是,如果您想手动创建或查看数据,JSON 是一个更友好(但性能较差)的选项。
有关 BSON 与 JSON 的更多上下文,您可能也对我在软件工程堆栈交换上的回答 Why does MongoDB check for order of keys when matching embedded documents? 感兴趣。
我假设用户输入 mongo shell 的所有命令在发送到 mongodb 服务器进行处理之前都被转换为 JSON 字符串.
例如,如果我将以下 JSON 发送到 mongodb 服务器,服务器将 return companyId 等于“12345”,这里是 JSON:
{"companyId": "12345"}
有没有办法让mongoshell显示发送到服务器进行处理的实际命令?
P.S。以防万一你对我上面的假设感到疑惑,我正在使用 mongo-cxx 驱动程序将命令发送到 mongodb 服务器。所有命令都被格式化并作为 JSON 个对象发送到 mongodb 服务器。
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.
您关于发送到服务器进行处理的原始命令的假设是不正确的:MongoDB wire protocol and server-side storage both use BSON(二进制 JSON 类序列化格式)。 JSON 是一种思考数据模型(以及处理供人类消费和创建的数据)的便捷方式,但 BSON 是 JSON 数据类型和行为的超集。
比如BSON是一种有序的二进制数据结构(不是字典),不需要字段名是唯一的。 BSON 还支持 additional data types 例如二进制数据、32 位和 64 位整数、浮点数和小数 (MongoDB 3.4+)。 MongoDB 驱动程序负责在不同表示之间序列化数据以方便使用(例如特定于语言的数据结构,JSON 或 BSON),但如果您将 MongoDB 视为 "BSON database"这可能有助于确定您的方法中的一些性能改进或解释一些其他不明显的行为,例如键的排序。
如果您真的想查看与 MongoDB 部署交换的原始对话,您可以使用像 Wireshark 这样理解 MongoDB 有线协议的网络数据包检查工具。但是,这不太可能有帮助,除非您正在尝试开发新的驱动程序或调试非常低级别的 server/driver 问题。
如果您对记录服务器收到的 MongoDB 查询或命令更感兴趣,您可以 adjust the server log level to include appropriate detail (caveat: additional debug detail can easily be overwhelming) or consider using the MongoDB profiler。在繁忙的生产环境中应谨慎使用增加的日志记录或分析级别,因为它们会增加显着的 I/O 和存储开销。
Is there a way to have mongo shell display the actual command that is sent to the server for processing?
没有显示发送到服务器的命令的内置选项,但您可以通过自定义 mongo
shell 和 JavaScript 来添加更多调试信息.
大多数常见的 mongo
shell 帮助器都是作为 JavaScript 函数实现的,这些函数将提供的参数转换为可以传递给 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 的数据结构。
如果你想看看 mongo
shell 如何实现一个特定的帮助程序来构造一个命令,例如 db.collection.insertOne()
,试试 运行 不带括号的命令:db.collection.insertOne
。大多数 shell 辅助逻辑将与参数验证和返回结果对象有关,但在某些时候,实际的服务器通信将委托给本机 C++ 方法,如果您尝试 eval,它将显示为 [native code]
mongo
shell 中的函数不带括号。
I am using the mongo-cxx driver to send commands to the mongodb server
默认情况下,C++ 驱动程序直接使用 BSON 数据结构并避免使用 document builder interfaces 进行不必要的序列化 to/from JSON。但是,如果您想手动创建或查看数据,JSON 是一个更友好(但性能较差)的选项。
有关 BSON 与 JSON 的更多上下文,您可能也对我在软件工程堆栈交换上的回答 Why does MongoDB check for order of keys when matching embedded documents? 感兴趣。