Definition定义
profileChanged in version 5.0.在版本5.0中的更改。For a对于mongodinstance, the command enables, disables, or configures the database profiler.mongod实例,该命令启用、禁用或配置数据库分析器。The profiler captures and records data on the performance of write operations, cursors, and database commands on a running分析器捕获并记录正在运行的mongodinstance. If the profiler is disabled, the command configures how slow operations are logged to the diagnostic log.mongod实例上写操作、游标和数据库命令的性能数据。如果禁用分析器,该命令将配置如何将慢速操作记录到诊断日志中。On在mongod, if the database profiler level is2, full logging is enabled on the profiler and thediagnostic log.mongod上,如果数据库分析器级别为2,则会在分析器和诊断日志上启用完整日志记录。At database profiler level在数据库分析器级别1, the following settings modify both the profiler and thediagnostic log:1,以下设置会修改分析器和诊断日志:If the database profiler level is如果数据库分析器级别为0, the database profiler is disabled.0,则禁用数据库分析器。At level在级别0the following settings only modify the diagnostic log:0,以下设置仅修改诊断日志:For a对于mongosinstance, the command only configures how operations get written to the diagnostic log.mongos实例,该命令仅配置如何将操作写入诊断日志。You cannot enable the database profiler on a您无法在mongosinstance becausemongosdoes not have any collections that the profiler can write to.mongos实例上启用数据库分析器,因为mongos没有分析器可以写入的任何集合。Starting in MongoDB 5.0, changes made to the database profiler从MongoDB 5.0开始,使用level,slowms,sampleRate, orfilterusing theprofilecommand ordb.setProfilingLevel()wrapper method are recorded in thelog file.profile命令或db.setProfilingLevel()包装器方法对数据库分析器level、slowms、sampleRate或filter所做的更改将记录在日志文件中。On在mongos, you can setprofilelevel to:mongos上,您可以将profile级别设置为:0to set the为诊断日志设置slowms,sampleRate, andfilterfor the diagnostic log;slowms、sampleRate和filter;-1to read the current settings.读取当前设置。
The profiler is off by default.默认情况下,分析器处于关闭状态。Warning
Profiling can degrade performance and expose unencrypted query data in the system log. Carefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.分析可能会降低性能,并在系统日志中暴露未加密的查询数据。在生产部署上配置和启用分析器之前,请仔细考虑任何性能和安全影响。See Profiler Overhead for more information on potential performance degradation.有关潜在性能下降的更多信息,请参阅Profiler开销。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
profile: <level>,
slowms: <threshold>,
sampleRate: <rate>,
filter: <filter expression>
}
)Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
profile | int |
|
slowms | int |
|
sampleRate |
| |
filter | object |
|
The db.getProfilingStatus() and db.setProfilingLevel() shell methods provide wrappers around the profile command.db.getProfilingStatus()和db.setProfilingLevel()shell方法为profile命令提供包装。
Behavior行为
The profile command obtains a write lock on the affected database while enabling or disabling the profiler. This is typically a short operation. The lock blocks other operations until the profile command has completed.profile命令在启用或禁用分析器时获得受影响数据库的写锁。这通常是一个简短的操作。锁会阻止其他操作,直到profile命令完成。
When connected to a sharded cluster through 当通过mongos, you can run the profile command against any database.mongos连接到分片集群时,您可以对任何数据库运行profile命令。
Example示例
Enable Filtering启用筛选
To enable profiling and filter the logged data:要启用分析并筛选记录的数据,请执行以下操作:
db.runCommand(
{
profile: 1,
filter:
{
$or:
[
{ millis: { $gte: 100 } },
{ user: "testuser@admin" }
]
}
}
)
The filter only selects operations that are:筛选器仅选择以下操作:
at least至少100milliseconds long, or100毫秒长,或submitted by the由testuser.testuser提交。
Unset a Filter取消设置筛选器
To clear a profile filter, run 要清除配置文件筛选器,请使用profile with the filter: "unset" option.filter: "unset"选项运行profile。
db.runCommand(
{
profile: 1,
filter: "unset"
}
)
The operation returns a document with the previous values for the settings.该操作将返回一个包含先前设置值的文档。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus().db.getProfilingStatus()。