db.setProfilingLevel()
On this page本页内容
- Definition定义
- Syntax语法
- Parameters参数
- Returns返回
- Behavior行为
- Examples实例
- Enable Profiler and Set Slow Operation Threshold and Sample Rate启用探查器并设置慢速操作阈值和采样率
- Disable Profiler and Set Slow Operation Threshold and Sample Rate禁用探查器并设置慢速操作阈值和采样率
- Set a Filter to Determine Profiled Operations设置筛选器以确定分析的操作
- Unset a Filter取消设置筛选器
Definition定义
- db.setProfilingLevel(level, options)
- Changed in version 5.05.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.- mongod实例上的写操作、游标和数据库命令的性能的数据。- If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.如果探查器被禁用,则该方法配置将慢速操作记录到诊断日志的方式。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 the- operationProfiling.modesetting or the- --profilecommand-line option.- 0(默认值),或由- operationProfiling.mode设置或- --profile命令行选项设置的值。- If the database profiler level is如果数据库探查器级别为- 1or- 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仅影响诊断日志。- With对于- mongosinstances, the method sets the- slowms,- sampleRateand- filterconfiguration settings, which configure how operations get written to the diagnostic log.- mongos实例,该方法设置- slowms、- sampleRate和- filter配置设置,这些设置配置如何将操作写入诊断日志。- You cannot enable the Database Profiler on a您无法在- mongosinstance because- mongosdoes not have any collections that the profiler can write to.- mongos实例上启用数据库探查器,因为- mongos没有任何探查器可以写入的集合。- The- profilelevel must be- 0for a- mongosinstance.- mongos实例的- profile级别必须为- 0。- Starting in MongoDB 4.4.2, you can specify a filter on both从MongoDB 4.4.2开始,您可以在- mongodand- mongosinstances to control which operations are logged by the profiler.- mongod和- mongos实例上指定一个- filter,以控制探查器记录哪些操作。- 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 (also available starting in 4.4.2, and 4.2.12), changes made to the database profiler从MongoDB 5.0开始(也可从4.4.2和4.2.12开始使用),使用- level,- slowms,- sampleRate, or- filterusing the- profilecommand or- db.setProfilingLevel()wrapper method are recorded in the- log file.- profile命令或- db.setProfilingLevel()包装方法对数据库探查器- level、- slowms、- sampleRate或- filter所做的更改将记录在日志文件中。
Syntax语法
The db.setProfilingLevel() method has the following form:db.setProfilingLevel()方法的形式如下:
db.setProfilingLevel(<level>, <options>)
Parameters参数
| level | integer | 
 
 | ||||||||
| options | document or integer | optionsargument instead of a document, the value is assigned to slowms. The following options are available:options参数而不是文档,则会将该值分配给slowms。以下选项可用:
 | 
Returns返回
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:其中:
- wasis the previous level setting.- was是以前的- level设置。
- slowms- is the previous slowms setting.是以前的- slowms设置。
- sampleRate- is the previous sampleRate setting.是以前的- sampleRate设置。
- filteris the previous filter setting. (New in MongoDB 4.4.2)- filter是以前的- filter设置。(MongoDB 4.4.2新增)
- noteis a string explaining the behavior of- filter.- note是一个解释- filter行为的字符串。- This field only appears in the output when只有当- filteris also present. (New in MongoDB 4.4.2)- filter也存在时,此字段才会出现在输出中。(MongoDB 4.4.2新增)
To view the current profiling level, see 要查看当前的评测级别,请参阅db.getProfilingStatus().db.getProfilingStatus()。
Behavior行为
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, and- 20毫秒,并且
- the sampleRate to- 0.42.- sampleRate为- 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慢操作阈值- 20milliseconds, and- slowms为- 20毫秒,并且
- the sampleRate to- 0.42.- sampleRate设置为- 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设置筛选器以确定分析的操作
New in version 4.4.2. 4.4.2版新增。
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 record- queryoperations that took longer than 2 seconds.- { op: "query", millis: { $gt: 2000 } }的- filter,这会导致探查器只记录耗时超过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.setProfileLevel()。
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()。