Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB CLI

Customize the MongoDB CLI Output

On this page

  • Go templates
  • json-path Output Type

You can customize the MongoDB CLI output fields and format using a Go template or a JSON path.

You can specify the Go template with the command for a simple output or through a separate file for a complex output. For information on the template, see Package template. For information on the types and properties available for each response, see:

You can specify a template with the command using the --output or -o option:

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

You can specify a template using a file using the --output or -o option:

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

The json-path output type limits the results of a command to the fields you specify.

When you add the --output option to a command, you can specify the type json-path. You must provide json-path with an expression to evaluate against your results, which means you must be aware of the json fields returned by your command.

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

json-path expressions refer to the JSON element that a MongoDB CLI command returns. The $ character represents the root element, which is usually an object or an array.

For a list of valid characters and their functions, see JSONPath expressions.

In the following example, a user retrieves their API keys with mongocli iam organizations apiKeys list. The json-path expression limits the output to the desc field of the first key, rather than returning the entire list of keys.

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

Running the same command with --output json returns the full JSON element from the API. It's important to understand the JSON structure returned by a command in order to operate on it with json-path.

Using the the following full JSON output for reference, the --output json-path with the expression $[0].desc finds and returns only the value "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"
},
]

In the following example, a user retrieves their API keys with mongocli iam organizations apiKeys list. The json-path expression limits the output to the desc field of the specific JSON object with id d2c9ccf2d1960e736a95d585.

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

In the following example, a user retrieves information for a private endpoint with mongocli-atlas-privateEndpoints-aws-describe. The json-path expression limits the output to the status field of the root element.

mongocli atlas privateendpoint aws describe 601a4044da900269480a2533 --output json-path='$.status'
> WAITING_FOR_USER
←  Enable Autocomplete for the MongoDB CLIQuick Start →