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

Atlas CLI 出力のカスタマイズ

Go テンプレートまたは JSON パスを使用して Atlas CLI の出力フィールドと形式をカスタマイズできるため、Atlas CLI の出力に基づいてプロセスを自動化するのが容易になります。

Goテンプレートは、Atlas CLI コマンド内または別のファイルを使用して指定できます。Goテンプレートの詳細については、パッケージ テンプレートを参照してください。各応答で使用可能なタイプとプロパティについては、Atlas のタイプ を参照してください。

--outputまたは-oオプションを使用して、 コマンドでテンプレートを指定できます。

--output|-o go-template="{{<template>}}"

あるいは、 --outputまたは-oオプションを使用して、 ファイルを介してテンプレートを指定することもできます。

--output|-o go-template-file="<path-to-template-file>"

次のコマンドは、テンプレートを使用して、指定された組織内のプロジェクトの数を取得します。

atlas projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template="Count: {{.TotalCount}}"

上記のコマンドは、次の出力を返します。

Count: 2

次のAtlas clusters describestring AtlasgetStartedコマンドは、テンプレートを使用して、 という名前の クラスターの接続 を取得します。Atlas へのアクセスには、デフォルトの プロファイルが使用されます。

atlas clusters describe getStarted -o go-template="Parse: {{.SrvAddress}}"

上記のコマンドは、次のようなstringを返します。

Parse: mongodb+srv://getstarted.example.mongodb.net

MongoDB Shellであるmongoshを使用して、srvAddress と接続stringを使用して getStarted クラスターに接続できます。 この例では、ユーザー名 User1 を持つユーザーに対して前回の コマンドによって返された接続stringを使用します。

mongo "mongodb+srv://getstarted.example.mongodb.net" --username User1 --password ChangeThisPasswordToSomethingSecure

json-path出力タイプは、指定したフィールドにコマンドの結果を制限します。

コマンドに--outputオプションを追加する場合、 json-pathタイプを指定できます。 結果を評価するには、 json-pathに 式 を指定する必要があります。つまり、コマンドによって返されるjsonフィールドに注意する必要があります。

<command> --output|-o json-path='$<expression>'

json-path 式は Atlas CLI コマンドが返す JSON 要素を指します。$ 文字はルート要素を表します。ルート要素は通常オブジェクトまたは配列です。

有効な文字とその関数のリストについては、JSONPath 式を参照してください。

次の例では、ユーザーが自分の API キーを Atlas 組織 apiKeys list を使用して検索します。json-path 式は、キーのリスト全体を返すのではなく、出力を最初のキーの desc フィールドに制限します。

atlas organizations apikeys list --output json-path='$[0].desc'> owner_key

で同じコマンドを実行すると、 --output jsonAPI から完全な JSON 要素が返されます。json-path を使用して操作するためには、コマンドから返される JSON 構造を理解する必要があります。

次の完全な JSON 出力を参照として使用すると、式 $[0].desc を使用する --output json-path は値 "owner_key" のみを検索して返します。

[ //``$`` represents the outer array. { // ``[0]`` refers to the first element in the array (using a 0-based index). "id": "60e736a95d585d2c9ccf2d19", "desc": "owner_key", //``.desc`` refers to the ``desc`` field of that element. "roles": [ { "orgId": "5d961a949ccf64b4e7f53bac", "roleName": "ORG_OWNER" } ], "privateKey": "********-****-****-c4e26334754f", "publicKey": "xtfmtguk" }, { "id": "d2c9ccf2d1960e736a95d585", "desc": "member_key", "roles": [ { "orgId": "5d961a949ccf64b4e7f53bac", "roleName": "ORG_MEMBER" } ], "privateKey": "********-****-****-c4e26334754f", "publicKey": "vfgcttku" },]

次の例では、ユーザーが自分の API キーを Atlas 組織 apiKeys list を使用して検索します。json-path 式は、id d2c9ccf2d1960e736a95d585 を持つ特定の JSON オブジェクトの desc フィールドに出力を制限します。

atlas organizations apikeys list --output json-path='$[? @.id=="d2c9ccf2d1960e736a95d585"].desc'> member_key

次の例では、ユーザーはAtlas privateEndpoints Amazon Web Services describeを使用してプライベートエンドポイントの情報を検索します。 json-path式は、出力をルート要素のstatusフィールドに制限します。

atlas privateendpoints aws describe 601a4044da900269480a2533 --output json-path='$.status'> WAITING_FOR_USER
このページを評価