Customize the MongoDB CLI Output Using a Go Template
You can customize the MongoDB CLI output fields and format using a Go
template. The template can be specified 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:
Syntax
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>"
Examples
For example, the following command uses the template to retrieve a count of the number of projects in the specified organization using the default profile:
mongocli iam projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template="Count: {{.TotalCount}}"
The preceding command returns the following output:
Count: 2
For example, consider the following file named template.tmpl
:
Projects: {{range .Results}}{{.ID}} {{end}}
The following command uses the template.tmpl
file to retrieve
the IDs of the projects in the specified organization using the
default profile:
mongocli iam projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template-file="template.tmpl"
The preceding command returns the following output:
Projects: 5e2211c17a3e5a48f5497de3 5f455b39749bea5fb575dccc