MongoDB通过用户HOME目录下的.mongorc.js或.mongoshrc.js文件加载提示符,默认此文件为空。
默认效果
MongoDB Enterprise replica02:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
db01 0.000GB
db02 0.000GB
encryption 0.000GB
local 0.000GB
sample_mflix 0.000GB
自动显示行号
[root]# cat .mongorc.js
let cmdCount = 1;
prompt = function() {
return (cmdCount++) + " > ";
}
效果展示:
1 > show collections
2 > use test
3 >
显示数据库和主机名
[root]# cat .mongorc.js
{
const hostnameSymbol = Symbol('hostname');
prompt = () => {
if (!db[hostnameSymbol])
db[hostnameSymbol] = db.serverStatus().host;
return `${db.getName()}@${db[hostnameSymbol]}> `;
};
}
效果展示:
admin@guizhou_hp-pop-10-150-57-13> show dbs
admin 0.000GB
config 0.000GB
db01 0.000GB
db02 0.000GB
encryption 0.000GB
local 0.000GB
sample_mflix 0.000GB
显示启动时间和集合数量
[root]# cat .mongorc.js
prompt = function() {
return "Uptime:" + db.serverStatus().uptime +
" Documents:" + db.stats().objects +
" > ";
}
效果展示:
Uptime:26122 Documents:3 > show dbs
admin 0.000GB
config 0.000GB
db01 0.000GB
db02 0.000GB
encryption 0.000GB
local 0.000GB
sample_mflix 0.000GB