对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db.setProfilingLevel()(mongosh方法)

db.setProfilingLevel(level, options)

在版本5.0中进行了更改。

对于 mongod 实例,该方法可启用、禁用或配置数据库分析器。分析器捕获并记录正在运行的 mongod 实例上的写入操作、游标和数据库命令性能的数据。如果分析器被禁用,该方法将配置如何将慢操作记录到诊断日志中。

注意

使用 db.setProfilingLevel() 对分析级别所做的更改不会保留。当服务器重新启动时,它会恢复为 0(默认值),或者由 operationProfiling.mode 设置或 --profile 命令行选项设置的值。

If the database profiler level is 1 or 2 (specifically, the database profiler is enabled), the slowms and sampleRate affect the behavior of both the profiler and the diagnostic log.

If the database profiler level is 0 (specifically, database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.

使用 mongos 实例,该方法设置 slowms、sampleRate 和 filter 配置设置,从而配置如何将操作写入诊断日志。您无法在 mongos 实例上启用数据库分析器,因为 mongos 没有分析器可以写入的任何集合。对于 mongos 实例,profile 等级必须为 0。

You can specify a filter on both mongod and mongos instances to control which operations are logged by the profiler. When you specify a filter for the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.

db.setProfilingLevel() provides a wrapper around the profile command.

Starting in MongoDB 5.0, changes made to the database profiler level, slowms, sampleRate, or filter using the profile command or db.setProfilingLevel() wrapper method are recorded in the log file.

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

重要

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.

The db.setProfilingLevel() method has the following form:

db.setProfilingLevel(<level>, <options>)
Parameter
类型
说明

整型

配置数据库分析器级别。以下分析器级别可用:

0

分析器已关闭,因此不收集任何数据。这是默认的分析器级别。

1

The profiler collects data for operations that exceed the slowms threshold or match a specified filter.

设置筛选器后:

  • slowms 和 sampleRate 选项不用于分析。

  • 分析器仅捕获与过滤器匹配的操作。

2

该分析器会收集所有操作的数据。

当设立为 2 级时,分析器会忽略用户提供的 slowms 和 filter 值。

Because profiling is not available on mongos, db.setProfilingLevel() cannot be used to set the profiling level to a value other than 0 on a mongos instance.

文档或整数

可选。接受整数或选项文档。如果将整数值作为 options 参数而不是文档传递,则该值将分配给 slowms。可以使用以下选项:

  • slowms:

    默认: 100
    类型: 整数

    慢速操作时间阈值(以毫秒为单位)。运行时间超过此阈值的操作被视为慢速操作。

    根据 workingMillis记录慢操作,这是 MongoDB 处理该操作所花费的时间。这意味着等待锁和流量控制等因素不会影响操作是否超过慢操作阈值。

    当 logLevel 设置为 0 时,MongoDB 会将慢速操作记录到诊断日志中,记录速度由 slowOpSampleRate 确定。

    在较高的 logLevel 设置下,所有操作都会出现在诊断日志中,无论其延迟如何,但从节点(secondary node from replica set)记录慢速 oplog 条目消息的操作除外。从节点(secondary node from replica set)仅日志慢速 oplog 条目;增加 logLevel 不会日志所有 oplog 条目。

    对于 mongod 实例,该设置会影响诊断日志,如果启用,还会影响分析器。

    对于 mongos 实例,该设置仅影响诊断日志,不影响分析器,因为在 mongos 上无法进行分析。

    此参数影响与配置文件选项 slowOpThresholdMs 相同的设置。

  • slowinprogms:

    8.3版本新增。

    默认: 5000
    类型: 整数

    正在进行的查询的慢 operation time 阈值(以毫秒为单位)。MongoDB运行时长超过此阈值的操作记录为正在进行的慢速查询。一旦查询操作超过时间阈值, MongoDB就会将查询为进行中的慢速查询。

    此参数影响与配置文件选项 slowOpInProgressThresholdMs 相同的设置。

  • 采样率:

    默认值:1.0
    类型:double

    要分析或日志的慢操作的比例。sampleRate 接受 0 到 1(含)之间的值。

  • 过滤器:

    类型:对象

    一个筛选器表达式,用于控制要分析和记录的操作。该筛选器表达式中的字段可以是分析器输出中的任何字段。

    对于 mongod 实例,该设置会影响诊断日志,如果启用,还会影响分析器。

    对于 mongos 实例,该设置仅影响诊断日志,不影响分析器,因为在 mongos 上无法进行分析。

    从 MongoDB 8.0 开始,您可以指定 workingMillis 作为过滤器参数,以根据 MongoDB 处理该操作所花费的时间来记录操作。

    如需用于控制已记录操作的过滤器示例,请参阅设置过滤器以确定已分析的操作。

    When a profiling filter is set, the slowms and sampleRate options do not affect the diagnostic log or the profiler.

该方法返回一个包含先前设置值的文档。

{
"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 is the previous level setting.

  • slowms 是之前的 slowms 设置。

  • sampleRate 是之前的 sampleRate 设置。

  • filter 是之前的过滤器设置。

  • note 是解释 filter 行为的字符串。只有当 filter 也存在时,此字段才会出现在输出中。

注意

filter 和 note 字段仅在先前的级别设置中存在时才会出现在输出中。

要查看当前分析级别,请参阅 db.getProfilingStatus()。

警告

分析会降低性能,并在系统日志中暴露未经加密的查询数据。在生产部署中配置和启用分析器之前,请仔细考虑对性能和安全的影响。

有关潜在性能下降的更多信息,请参阅分析器开销。

以下示例集用于 mongod 实例:

db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })

该操作返回一个包含先前设置值的文档。

要查看当前分析级别,请参阅 db.getProfilingStatus()。

下面的示例为 mongod 或 mongos 实例进行了设置:

db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })

该操作返回一个包含先前设置值的文档。

要查看当前分析级别,请参阅 db.getProfilingStatus()。

以下示例集用于 mongod 实例:

  • the profiling level to 1,

  • a filter of { op: "query", millis: { $gt: 2000 } }, which causes the profiler to only record query operations that took longer than 2 seconds.

db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )

该操作返回一个包含先前设置值的文档。

要查看当前分析级别,请参阅 db.getProfilingStatus()。

You can set a filter without enabling the database profiler. The following example uses a filter with a profiling level of 0.

db.setProfilingLevel( 0, { filter: { op: "query", millis: { $gt: 2000 } } } )

由于分析级别为 0,分析器已禁用,因此不会将匹配的操作记录到分析器集合。过滤器仍然应用于诊断日志。

该操作返回一个包含先前设置值的文档。

要查看当前分析级别,请参阅 db.getProfilingStatus()。

要清除分析筛选器,请运行带有 filter: "unset" 选项的 db.setProfilingLevel()。

db.setProfilingLevel( 1, { filter: "unset" } )

该操作返回一个包含先前设置值的文档。

要查看当前分析级别,请参阅 db.getProfilingStatus()。