您可以为 MongoDB CLI 命令启用自动完成功能,以直接在 shell 中查看可用命令及其语法。
启用自动完成功能
If you installed the MongoDB CLI with Homebrew, autocomplete functionality is included with the installation. You must configure your shell to enable its completion support. To enable autocomplete for your shell, refer to the Homebrew Documentation.
如果 不是与MongoDB CLI Homebrew 一起安装的,则启用自动完成的过程取决于运行shell 的MongoDB CLI 。
Z shell
如果使用 zsh
运行MongoDB CLI ,请运行以下命令以在当前shell中启用自动完成功能:
echo "autoload -U compinit; compinit" >> ~/.zshrc
要为所有未来的shell会话启用自动完成功能,请执行以下操作:
如果使用 MacOS,请运行以下命令:
mongocli completion zsh > /usr/local/share/zsh/site-functions/_mongocli 如果使用Linux,请运行以下命令:
mongocli completion zsh > "${fpath[1]}/_mongocli"
启动新的shell以使设置生效。
Bash
如果您使用 Bash运行MongoDB CLI ,请通过操作系统的包管理器安装bash-completion
包。 然后运行以下命令,在当前shell中启用自动完成功能:
source <(mongocli completion bash)
要为所有未来的shell会话启用自动完成功能,请执行以下操作:
如果使用 MacOS,请运行以下命令:
mongocli completion bash > /usr/local/etc/bash_completion.d/mongocli 如果使用Linux,请运行以下命令:
mongocli completion bash > /etc/bash_completion.d/mongocli
启动新的shell以使设置生效。
鱼
如果您使用 fish
运行MongoDB CLI ,请运行以下命令以在当前shell中启用自动完成功能:
mongocli completion fish | source
要为所有未来的shell会话启用自动完成功能,请执行以下操作:
运行以下命令:
mongocli completion fish > ~/.config/fish/completions/mongocli.fish 启动新的shell以使设置生效。
PowerShell
如果使用 PowerShell运行MongoDB CLI ,请运行以下命令以在当前shell中启用自动完成功能:
PS C:\> mongocli completion powershell | Out-String | Invoke-Expression
To enable autocomplete for all future shells, add the output of the above command to your PowerShell profile. To learn how to edit a PowerShell profile, see Windows Documentation.
使用Tab
自动完成命令
配置自动完成功能后,您可以通过在 shell 中键入mongocli
并按 Tab
键来自动完成命令。 按多次Tab
可在可用的自动完成选项之间循环。
您可以在编写命令的任何阶段按Tab
,查看可用的自动完成选项。
示例
当您在 Shell 中键入mongocli
并按Tab
时,输出将类似于以下内容:
atlas -- Atlas operations. cloud-manager -- Cloud Manager operations. completion -- Generate shell completion scripts config -- Configure a profile to store access settings for your MongoDB deployment. help -- Help about any command iam -- Organization and projects operations. ops-manager -- Ops Manager operations.
如果您键入特定属性(例如atlas
作为命令的一部分,则可以看到所选属性的自动完成选项。
例如,当您在 Shell 中键入mongocli atlas
并按Tab
时,输出将类似于以下内容:
accessLists -- Manage the IP access list for your project. accessLogs -- Manage the access logs of a cluster. alerts -- Manage alerts for your project. backups -- Manage backups for your project. clusters -- Manage clusters for your project. dbusers -- Manage database users for your project. events -- Manage events for your project. logs -- Download host logs for your project. metrics -- Get measurements on the state of the MongoDB process. processes -- Manage MongoDB processes for your project.