Customize the Prompt

You may modify the content of the prompt by setting the variable prompt in the mongo shell. The promptvariable can hold strings as well as JavaScript code. If prompt holds a function that returns a string, mongo can display dynamic information in each prompt.

You can add the logic for the prompt in the .mongorc.js file to set the prompt each time you start up the mongo shell.

Customize Prompt to Display Number of Operations

For example,to create a mongo shell prompt with the number of operations issued in the current session, define the following variables in the mongo shell:

> cmdCount = 1;
1
> prompt = function() {
... return (cmdCount++) + "> ";
... }
function () {
return (cmdCount++) + "> ";
}

The prompt would then resemble the following:

1>
2>
3>

Customize Prompt to Display Database and Hostname

To create a mongo shell prompt in the form of <database>@<hostname>$, define the following variables:

3> prompt = "> "
> host = db.serverStatus().host;
huey
> prompt = function() {
... return db+"@"+host+"$ ";
... }
function () {
return db+"@"+host+"$ ";
}

The prompt would then resemble the following:

test@huey$ 

Customize Prompt to Display Up Time and Document Count

To create a mongo shell prompt that contains the system up time and the number of documents in the current database, define the following prompt variable in the mongo shell:

test@huey$ prompt = "> "
>
> prompt = function() {
... return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
... }
function () {
return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
}

The prompt would then resemble the following:

Uptime:5897 Documents:6 >

Use an External Editor in the mongo Shell

You can use your own editor in the mongo shell by setting the EDITOR environment variable before starting the mongo shell.

export EDITOR=vim
mongo

Once in the mongo shell, you can edit with the specified editor by typing edit <variable> or edit <function>, as in the following example:

  1. Define a function myFunction:

    function myFunction () { }
  2. Edit the function using your editor:
    edit myFunction

    The command should open the vim edit session. When finished with the edits, save and exit vim edit session.

  3. In the mongo shell, type myFunction to see the function definition:
    myFunction

    The result should be the changes from your saved edit:

    function myFunction() {
    print("This was edited");
    }

NOTE: As mongo shell interprets code edited in an external editor, it may modify code in functions, depending on the JavaScript compiler. For mongo may convert 1+1 to 2 or remove comments. The actual changes affect only the appearance of the code and will vary based on the version of JavaScript used but will not affect the semantics of the code.

Change the mongo Shell Batch Size

The db.collection.find() method is the JavaScript method to retrieve documents from a collection. The db.collection.find() method returns a cursor to the results; however, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. The mongo shell will prompt Type it to iterate another 20 times.

You can set the DBQuery.shellBatchSize attribute to change the number of documents from the default value of 20, as in the following example which sets it to 10:

DBQuery.shellBatchSize = 10;

最新文章

  1. 使用 Git Hooks 实现自动项目部署
  2. Vuforia判断当识别追踪的对象
  3. PHP造PDO对象和事务功能
  4. 使用angular.js开发的一个简易todo demo
  5. 怀念我的老师——丁伟岳院士 by 史宇光
  6. 1136. Parliament(二叉树)
  7. HDU 3392 Pie(DP)
  8. 浙大PTA - - File Transfer
  9. Android之声音管理器《AudioManager》的使用以及音量控制
  10. Js之History对象
  11. 5、Spring+Struts2+MyBatis+分页(mybatis无代理)增删改查
  12. IOS开发-UI学习-沙盒机制&amp;文件操作
  13. selenium中CSS选择器定位
  14. docker nginx letsencrypt
  15. Anaconda3下安装Anaconda2
  16. 如何将Anaconda更新到想要的python版本
  17. Java定时任务示例
  18. Hadoop介绍-3.HDFS介绍和YARN原理介绍
  19. 微软Xbox One无线手柄控制机器人
  20. llg的农场(farm)

热门文章

  1. The IAR Archive Tool—iarchive
  2. Codeforces Gym 100463E Spies 并查集
  3. js验证身份证id
  4. HDU 1504 Disk Tree
  5. iOS开发——UI篇Swift篇&amp;UIToolbar
  6. javascript exec方法
  7. javascript实现单例模式
  8. QT程序在windows下部署发布
  9. uva 12096 The SetStack Computer
  10. SimpleUrlHandlerMapping 处理器映射的配置--转