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

自定义 MongoDB CLI 输出

您可以使用Go模板或 JSON 路径自定义 MongoDB CLI 输出字段和格式。

您可以使用命令为简单输出指定Go模板,也可以通过单独的文件为复杂输出指定 Go 模板。有关模板的信息,请参阅包模板。有关每个响应可用的类型和属性的信息,请参阅:

您可以通过使用--output或-o选项的命令来指定模板:

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

您可以使用--output或-o选项使用文件指定模板:

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

以下命令使用该模板检索指定组织中使用默认配置文件的项目计数:

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

上述命令会返回以下输出:

Count: 2

以下mongocli-atlas-clusters-describe命令使用该模板检索名为getStarted的 Atlas 集群的连接字符串。 它使用默认配置文件访问 Atlas。

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

上一个命令会返回一个类似于以下内容的string :

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

您可以使用MongoDB Shell mongosh ,通过 srvAddress 和连接string连接到 getStarted集群。 此示例使用上一个命令返回的连接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 表达式是指 MongoDB CLI 命令返回的 JSON 元素。$ 字符表示根元素,通常是对象或数组。

有关有效字符及其功能的列表,请参阅 JSONPath 表达式。

在以下示例中,用户使用 mongocli iam organizations apiKeys list 检索其API密钥。 json-path表达式将输出限制为第一个键的 desc字段,而不是返回整个键列表。

mongocli iam organization apikeys list --output json-path='$[0].desc'> owner_key

使用--output json 运行相同的命令会返回来自 API 的完整 JSON 元素。必须了解命令返回的 JSON 结构,以便使用 json-path 对其进行操作。

以下完整的 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" },]

在以下示例中,用户使用 mongocli iam organizations apiKeys list 检索其API密钥。 json-path表达式使用 id d2c9ccf2d1960e736a95d585 将输出限制为特定JSON对象的 desc字段。

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

在以下示例中,用户使用mongocli-atlas-privateEndpoints-aws-describe检索私有端点的信息。 json-path表达式将输出限制为根元素的status字段。

mongocli atlas privateendpoint aws describe 601a4044da900269480a2533 --output json-path='$.status'> WAITING_FOR_USER
给本页内容打分