On this page本页内容
db.setProfilingLevel(level, options)
Changed in version 5.0.在版本5.0中更改。
For a 对于mongod
instance, 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 探查器在运行的mongod
instance. mongod
实例上捕获并记录有关写操作、游标和数据库命令性能的数据。If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.如果禁用探查器,该方法将配置将操作记录到诊断日志中的速度。
If the database profiler level is 如果数据库探查器级别为1
or 2
(specifically, the database profiler is enabled), the slowms, sampleRate affect the behavior of both the profiler and the diagnostic 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
仅影响诊断日志。
Starting in MongoDB 4.0, for a 从MongoDB 4.0开始,对于mongos
instance, the method sets the slowms
, sampleRate
and filter
configuration settings, which configure how operations get written to the diagnostic log. mongos
实例,该方法设置slowms
、sampleRate
和`配置设置,这些设置配置如何将操作写入诊断日志。You cannot enable the Database Profiler on a 无法在mongos
instance because mongos
does not have any collections that the profiler can write to. mongos
实例上启用数据库探查器,因为mongos
没有探查器可以写入的任何集合。The profile
level must be 0
for a mongos
instance.mongos
实例的profile
级别必须为0。
Starting in MongoDB 4.4.2, you can specify a filter on both 从MongoDB 4.4.2开始,您可以在mongod
and mongos
instances to control which operations are logged by the profiler. mongod
和mongos
实例上指定一个筛选器,以控制探查器记录哪些操作。When you specify a 为探查器指定filter
for 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 提供profile
command.profile
命令的包装。
Starting in MongoDB 5.0 (also available starting in 4.4.2, 4.2.12, and 4.0.22), changes made to the database profiler 从MongoDB 5.0(也可从4.4.2、4.2.12和4.0.22开始使用)开始,使用level
, slowms
, sampleRate
, or filter
using the profile
command or db.setProfilingLevel()
wrapper method are recorded in the log file
.profile
命令或db.setProfilingLevel()
包装方法对数据库探查器level
、slowms
、sampleRate
或filter
所做的更改将记录在日志文件中。
The db.setProfilingLevel()
method has the following form:db.setProfilingLevel()
方法具有以下形式:
db.setProfilingLevel(<level>, <options>)
level | integer |
| ||||||||
options | document or integer |
|
The method returns a document that contains the previous values of the settings.该方法返回包含设置的先前值的文档。
{ "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 }
{ "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" : NumberLong("6755945537557495811") } }, "operationTime" : Timestamp(1572991238, 1) }
{ "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" : NumberLong("6755946491040235540") } } }
Where:
was
slowms
sampleRate
filter
note
filter
. filter
行为的字符串。filter
is also present. (New in MongoDB 4.4.2)filter
也存在时,此字段才会出现在输出中。(MongoDB 4.4.2新增)The filter
and note
fields only appear in the output if they were present in the previous level setting.filter
和注释字段仅在先前级别设置中出现时才会显示在输出中。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus()
.db.getProfilingStatus()
。
Profiling can impact performance and shares settings with 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.有关潜在性能下降的更多信息,请参阅探查器开销。
The following example sets for a 以下示例为mongod
instance:mongod
实例设置:
1
,1
,20
milliseconds, andslowms
至20
毫秒0.42
.sampleRate
设置为0.42
。db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
The method returns a document with the previous values for the settings.该方法返回一个文档,其中包含设置的先前值。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus()
.db.getProfilingStatus()
。
The following example sets for a 以下示例为mongod
or mongos
instance:mongod
或mongos
实例设置:
0
,0
,20
milliseconds, andslowms
至20
毫秒0.42
.sampleRate
设置为0.42
。db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
The method returns a document with the previous values for the settings.该方法返回一个文档,其中包含设置的先前值。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus()
.db.getProfilingStatus()
。
New in version 4.4.2.在版本4.4.2中新增。
The following example sets for a 以下示例为mongod
instance:mongod
实例设置:
2
,{ op: "query", millis: { $gt: 2000 } }
, which causes the profiler to only record query
operations that took longer than 2 seconds.{ op: "query", millis: { $gt: 2000 } }
的筛选器,这将导致探查器只记录花费超过2秒的query
操作。db.setProfilingLevel( 2, { filter: { op: "query", millis: { $gt: 2000 } } } )
The method returns a document with the previous values for the settings.该方法返回一个文档,其中包含设置的先前值。
To view the current profiling level, see 要查看当前的分析级别,请参阅db.getProfilingStatus()
.db.getProfilingStatus()
。