定義
db.setProfilingLevel(level, options)バージョン5.0で変更。
mongodインスタンスの場合、メソッドはデータベースプロファイラーを有効化、無効化、または構成します。プロファイラーは、実行中のmongodインスタンスでの書き込み操作、カーソル、およびデータベースコマンドのパフォーマンスに関するデータをキャプチャして記録します。プロファイラーが無効の場合、このメソッドは、低速操作を診断ログに記録する方法を構成します。注意
db.setProfilingLevel()でプロファイリング レベルに加えられた変更は保持されません。サーバーを再起動すると、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.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
mongodandmongosinstances to control which operations are logged by the profiler. When you specify afilterfor the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.db.setProfilingLevel()provides a wrapper around theprofilecommand.MongoDB5.0 以降では、 コマンドまたは ラッパーメソッドを使用してデータベースプロファイラ
level、slowms、sampleRate、または に加えられた変更は、filterprofiledb.setProfilingLevel()log fileに記録されます。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
重要
このコマンドは、M0 および Flex クラスターではサポートされていません。詳細については、サポートされていないコマンド を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
The db.setProfilingLevel() method has the following form:
db.setProfilingLevel(<level>, <options>)
パラメーター
Parameter | タイプ | 説明 | ||||||
|---|---|---|---|---|---|---|---|---|
integer | データベースプロファイラーレベルを設定します。次のプロファイラー レベルを使用できます。
Because profiling is not available on | |||||||
ドキュメントまたは整数 | Optional. Accepts an integer or an options document. If an integer value is passed as the
|
戻り値
このメソッドは、前の設定値を含むドキュメントを返します。
{ "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" : Long("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" : Long("6755946491040235540") } } }
以下の条件に一致するもの。
wasが、前のレベル設定です。slowmsが、前の slowms 設定です。sampleRateが、前の sampleRate 設定です。filterは、前のフィルター設定です。noteは、filterの動作を説明する文字列です。このフィールドは、filterも存在する場合にのみ出力に表示されます。
注意
filter フィールドと note フィールドは、前のレベル設定に存在していた場合にのみ出力に表示されます。
現在のプロファイリング レベルを表示するには、db.getProfilingStatus() を参照してください。
動作
警告
プロファイリングにより、パフォーマンスが低下し、暗号化されていないクエリ データがシステム ログに公開されることがあります。プロファイラーを本番環境に設定して有効にする前に、パフォーマンスとセキュリティへの影響を慎重に検討してください。
パフォーマンス低下の可能性の詳細については、「プロファイラーのオーバーヘッド」を参照してください。
例
プロファイラーの有効化および低速操作のしきい値とサンプルレートの設定
次の例では、以下の値を mongodインスタンスに設定します。
the profiling level to
1,the slow operation threshold slowms to
20milliseconds, andthe sampleRate to
0.42.
db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
この操作は、前の設定値を含むドキュメントを返します。
現在のプロファイリング レベルを表示するには、db.getProfilingStatus() を参照してください。
プロファイラーの無効化および低速操作のしきい値とサンプルレートの設定
次の例では、以下の値を mongod インスタンスまたは mongos インスタンス用に設定します。
the profiling level to
0,the slow operation threshold slowms to
20milliseconds, andthe sampleRate to
0.42.
db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
この操作は、前の設定値を含むドキュメントを返します。
現在のプロファイリング レベルを表示するには、db.getProfilingStatus() を参照してください。
プロファイルされた操作を決定するためにフィルターを設定する
次の例では、以下の値を mongodインスタンスに設定します。
the profiling level to
1,
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() を参照してください。