AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

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.

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

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

db.setProfilingLevel(<level>, <options>)
Parameter
タイプ
説明

integer

データベースプロファイラーレベルを設定します。次のプロファイラー レベルを使用できます。

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.

ドキュメントまたは整数

Optional. Accepts an integer or an options document. If an integer value is passed as the options argument instead of a document, the value is assigned to slowms. The following options are available:

  • スローミリ秒:

    デフォルト: 100
    型: 整数

    低速操作時間のしきい値(ミリ秒単位)。操作が実行される時間がこのしきい値より長いと、低速とみなされます。

    低速操作は、MongoDB でその操作にかかった時間である workingMillis に基づいてログに記録されます。つまり、ロック待ちやフロー制御などの要因は、操作が低速操作のしきい値を超えるかどうかに影響しません。

    logLevelが0に設定されている場合、MongoDB は slowOpSampleRate で決定されたレートで低速操作を診断ログに記録します

    logLevel設定を引き上げると、レイテンシに関係なく、すべての操作が診断ログに表示されます。ただし、セカンダリによる低速の oplog エントリ メッセージのログ記録は除きます。セカンダリでは、低速の oploglogLevel エントリのみがログに記録されます。 を引き上げても、すべての oplog エントリがログに記録されるわけではありません。

    mongod インスタンスの場合、この設定は診断ログと(有効になっている場合)プロファイラーの両方に影響します。

    mongos インスタンスの場合、プロファイリングは mongos では利用できないため、設定は診断ログにのみ影響し、プロファイラーには影響しません。

    この引数は、構成ファイル オプション slowOpThresholdMs と同じ設定に影響します。

  • slowinprogms:

    バージョン8.3の新機能。

    デフォルト: 5000
    型: 整数

    進行中のクエリの低速操作時間のしきい値(ミリ秒単位)。MongoDB は、実行時間がこのしきい値より長い時間がかかる操作を、低速進行中のクエリとして記録します。MongoDB は、クエリ操作が時間しきい値を超えた時点で、クエリを進行中の低速クエリとしてログに記録します。

    この引数は、構成ファイル オプション slowOpInProgressThresholdMs と同じ設定に影響します。

  • sampleRate:

    デフォルト: 1.0
    型: double

    プロファイリングまたはログに記録する低速操作の割合。sampleRate は、0 から 1 までの値(両端を含む)を受け入れます。

  • フィルター:

    型: オブジェクト

    どの操作をプロファイリングしてログ記録するかを制御するフィルター式。フィルター式のフィールドは、プロファイラー出力内の任意のフィールドにできます。

    mongod インスタンスの場合、この設定は診断ログと(有効になっている場合)プロファイラーの両方に影響します。

    mongos インスタンスの場合、プロファイリングは mongos では利用できないため、設定は診断ログにのみ影響し、プロファイラーには影響しません。

    MongoDB 8.0 以降では、フィルターパラメータとして workingMillis を指定して、MongoDB がその操作に費やした時間に基づいて操作をログに記録できます。

    For an example of a filter used to control logged operations, see Set a Filter to Determine Profiled Operations.

    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() を参照してください。