Customize the mongosh
Prompt自定义mongosh
提示
mongosh
PromptOn this page本页内容
By default the 默认情况下,mongosh
prompt includes the current database name. mongosh
提示包括当前数据库名称。You can modify the 您可以修改prompt
variable to display custom strings or to return dynamic information about your mongosh
session.prompt
变量以显示自定义字符串或返回有关mongosh
会话的动态信息。
Custom prompts are not stored when you exit 退出mongosh
. mongosh
时不会存储自定义提示。To have a custom prompt persist through restarts, add the code for your custom prompt to .mongoshrc.js.要在重新启动时保持自定义提示,请将自定义提示的代码添加到.mongoshrc.js中。
Display Line Numbers显示行号
To display line numbers in the 要在mongosh
prompt, run the following code inside mongosh
:mongosh
提示符中显示行号,请在mongoh
内运行以下代码:
let cmdCount = 1;
prompt = function() {
return (cmdCount++) + "> ";
}
The prompt will look like this:提示如下所示:
1> show collections
2> use test
3>
Display Database and Hostname显示数据库和主机名
The current database name is part of the default 当前数据库名称是默认mongosh
prompt. mongosh
提示的一部分。To reformat the prompt to show the database and hostname, use a function like this one:要重新格式化提示以显示数据库和主机名,请使用以下函数:
{
const hostnameSymbol = Symbol('hostname');
prompt = () => {
if (!db[hostnameSymbol])
db[hostnameSymbol] = db.serverStatus().host;
return `${db.getName()}@${db[hostnameSymbol]}> `;
};
}
The prompt will look like this:提示如下所示:
admin@centos0722:27502>
Display System Up Time and Document Count显示系统启动时间和文档计数
To create a prompt that shows the system uptime and a count of documents across all collections in the current database, use a function like this one:要创建一个显示系统正常运行时间和当前数据库中所有集合的文档计数的提示,请使用以下函数:
prompt = function() {
return "Uptime:" + db.serverStatus().uptime +
" Documents:" + db.stats().objects +
" > ";
}