Definition定义
db.setProfilingLevel(level, options)Changed in version 5.0.在版本5.0中的更改。For a对于mongodinstance, the method 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 method configures how slow operations are logged to the diagnostic log.mongod实例上写操作、游标和数据库命令的性能数据。如果禁用分析器,该方法将配置如何将慢速操作记录到诊断日志中。Note
Changes made to the profiling level with使用db.setProfilingLevel()do not persist.db.setProfilingLevel()对分析级别所做的更改不会持久。When the server restarts, it reverts to当服务器重新启动时,它将恢复为0(the default), or the value set by either theoperationProfiling.modesetting or the--profilecommand-line option.0(默认值),或由operationProfiling.mode设置或--profile命令行选项设置的值。If the database profiler level is如果数据库分析器级别为1or2(specifically, the database profiler is enabled), the slowms and sampleRate affect the behavior of both the profiler and thediagnostic log.1或2(具体来说,数据库分析器已启用),则slowms和sampleRate会影响分析器和诊断日志的行为。If the database profiler level is如果数据库分析器级别为0(specifically, database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.0(具体来说,数据库分析器已禁用),则slowms和sampleRate仅影响诊断日志。With对于mongosinstances, the method sets theslowms,sampleRateandfilterconfiguration settings, which configure how operations get written to the diagnostic log.mongos实例,该方法设置slowms、sampleRate和filter配置设置,这些设置配置了如何将操作写入诊断日志。You cannot enable the Database Profiler on a您无法在mongosinstance becausemongosdoes not have any collections that the profiler can write to.mongos实例上启用数据库分析器,因为mongos没有分析器可以写入的任何集合。Theprofilelevel must be0for amongosinstance.mongos实例的profile级别必须为0。You can specify a filter on both您可以在mongodandmongosinstances to control which operations are logged by the profiler.mongod和mongos实例上指定一个筛选器,以控制分析器记录哪些操作。When you specify a为分析器指定filterfor the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.filter时,slowms和sampleRate选项不用于分析和慢速查询日志行。db.setProfilingLevel()provides a wrapper around the为profilecommand.profile命令提供包装。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所做的更改将记录在日志文件中。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令。
- 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 db.setProfilingLevel() method has the following form:db.setProfilingLevel()方法具有以下形式:
db.setProfilingLevel(<level>, <options>)Parameters参数
level |
| |||||||
options |
|
Returns值
The method returns a document that contains the previous values of the settings.该方法返回一个包含先前设置值的文档。
Standalone
{
"was" : 2,
"slowms" : 100,
"sampleRate" : 1,
"filter" : {
"$and" : [
{
"op" : {
"$eq" : "query"
}
},
{
"millis" : {
"$gt" : 20000
}
}
]
},
"note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",
"ok" : 1
}Replica Set Member
{
"was" : 0,
"slowms" : 100,
"sampleRate" : 1,
"filter" : {
"$and" : [
{
"op" : {
"$eq" : "query"
}
},
{
"millis" : {
"$gte" : 2000
}
}
]
},
"note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1572991238, 1),
"signature" : {
"hash" : BinData(0,"hg6GnlrVhV9MAhwWdeHmHQ4T4qU="),
"keyId" : Long("6755945537557495811")
}
},
"operationTime" : Timestamp(1572991238, 1)
}mongos Instance
{
"was" : 0,
"slowms" : 100,
"sampleRate" : 1,
"filter" : {
"$and" : [
{
"op" : {
"$eq" : "query"
}
},
{
"millis" : {
"$gte" : 2000
}
}
]
},
"note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",
"ok" : 1,
"operationTime" : Timestamp(1572991499, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1572991499, 2),
"signature" : {
"hash" : BinData(0,"nhCquIxUw7thlrBudXe3PnsnvP0="),
"keyId" : Long("6755946491040235540")
}
}
}Where:其中
wasis the previous level setting.slowmsis the previous slowms setting.sampleRateis the previous sampleRate setting.filteris the previous filter setting.noteis a string explaining the behavior of是一个解释filter.filter行为的字符串。This field only appears in the output when只有当filteris also present.filter也存在时,此字段才会出现在输出中。
Note
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus().db.getProfilingStatus()。
Behavior行为
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开销。
Examples示例
Enable Profiler and Set Slow Operation Threshold and Sample Rate启用分析器并设置慢速操作阈值和采样率
The following example sets for a 以下示例设置了一个mongod instance:mongod实例:
the profiling level to将分析级别设置为1,1,the slow operation threshold slowms to慢速操作阈值slowms为20milliseconds, and20毫秒,以及the sampleRate to采样率为0.42。0.42.
db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
The operation returns a document with the previous values for the settings.该操作将返回一个包含先前设置值的文档。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus().db.getProfilingStatus()。
Disable Profiler and Set Slow Operation Threshold and Sample Rate禁用分析器并设置慢速操作阈值和采样率
The following example sets for a 以下示例为mongod or mongos instance:mongod或mongos实例设置:
the profiling level to将分析级别设置为0,0,the slow operation threshold slowms to慢速操作阈值slowms为20milliseconds, and20毫秒,以及the sampleRate to采样率为0.42.0.42。
db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
The operation returns a document with the previous values for the settings.该操作将返回一个包含先前设置值的文档。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus().db.getProfilingStatus()。
Set a Filter to Determine Profiled Operations设置筛选器以确定配置文件操作
The following example sets for a 以下示例设置了一个mongod instance:mongod实例:
the profiling level to将分析级别设置为1,1,a filter of{ op: "query", millis: { $gt: 2000 } }, which causes the profiler to only recordqueryoperations that took longer than 2 seconds.{ op: "query", millis: { $gt: 2000 } }的筛选器,它使分析器只记录耗时超过2秒的query操作。
db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )
The operation returns a document with the previous values for the settings.该操作将返回一个包含先前设置值的文档。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus().db.getProfilingStatus().。
Unset a Filter取消设置筛选器
To clear a profile filter, run 要清除配置文件筛选器,请使用db.setProfilingLevel() with the filter: "unset" option.filter: "unset"选项运行db.setProfilingLevel() 。
db.setProfilingLevel( 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()。