|
2 | 2 | Interacting with the Mongo Shell
|
3 | 3 | ================================
|
4 | 4 |
|
5 |
| - |
6 | 5 | Introduction
|
7 | 6 | ------------
|
8 | 7 |
|
9 |
| -The MongoDB shell is a interactive javascript shell interface to MongoDB. It is invoked by running `mongo` or `mongo.exe`. |
10 |
| - |
11 |
| - |
| 8 | +The MongoDB shell is a interactive javascript shell interface to |
| 9 | +MongoDB. It is invoked by running :program:`mongo` or ``mongo.exe``. |
12 | 10 |
|
13 | 11 | Keyboard Shortcuts
|
14 | 12 | ------------------
|
15 | 13 |
|
16 |
| -The MongoDB shell tries to follow familiar keyboard shortcuts as found in bash and emacs. |
| 14 | +The MongoDB shell tries to follow familiar keyboard shortcuts as found |
| 15 | +in bash and emacs. |
17 | 16 |
|
18 |
| -The following table illustrates the various keystrokes supported by the MongoDB shell: |
| 17 | +The following table illustrates the various keystrokes supported by the |
| 18 | +MongoDB shell: |
19 | 19 |
|
20 |
| -You will notice that we try to accommodate multiple keybinding styles and as a result some functions have multiple bindings that will work. |
| 20 | +You will notice that MongoDB accommodates multiple keybinding styles, |
| 21 | +and as a result some functions have multiple bindings that will work. |
21 | 22 |
|
22 | 23 | =================== =====================================
|
23 | 24 | Keystroke Function
|
@@ -70,71 +71,89 @@ Meta-> end-of-history
|
70 | 71 | Custom Prompt
|
71 | 72 | -------------
|
72 | 73 |
|
73 |
| -The shell's prompt can be customized by creating variable 'prompt' in the shell. It can be any arbitrary javascript, including a function that returns a string. This flexibility allows for additional information to be displayed in the prompt. |
| 74 | +The shell's prompt can be customized by creating the variable ``prompt`` |
| 75 | +in the shell. It can be any arbitrary javascript, including a function |
| 76 | +that returns a string. This flexibility allows for additional |
| 77 | +information to be displayed in the prompt. |
74 | 78 |
|
75 |
| -The following three examples should give you a good enough idea to get you started. |
| 79 | +The following three examples should give you a good enough idea to get |
| 80 | +you started. |
76 | 81 |
|
77 |
| -A prompt that contains the number of commands issued: |
| 82 | +.. example:: |
78 | 83 |
|
79 |
| -.. code-block:: javascript |
| 84 | + A prompt that contains the number of commands issued: |
80 | 85 |
|
81 |
| - > cmdCount = 1; |
82 |
| - > prompt = function() { |
83 |
| - ... return (cmdCount++) + "> "; |
84 |
| - ... } |
85 |
| - 1> command |
86 |
| - 2> anothercommand |
87 |
| - 3> |
| 86 | + .. code-block:: javascript |
88 | 87 |
|
89 |
| -To make the prompt look a bit more familiar, we can make it database@host$: |
| 88 | + > cmdCount = 1; |
| 89 | + > prompt = function() { |
| 90 | + ... return (cmdCount++) + "> "; |
| 91 | + ... } |
| 92 | + 1> command |
| 93 | + 2> anothercommand |
| 94 | + 3> |
90 | 95 |
|
91 |
| -.. code-block:: javascript |
| 96 | +.. example:: |
92 | 97 |
|
93 |
| - > host = db.serverStatus().host; \\ since host should not change |
94 |
| - > prompt = function() { |
95 |
| - ... return db+"@"+host+"$ "; |
96 |
| - ... } |
97 |
| - |
98 |
| - switched to db monkeys |
99 |
| - |
| 98 | + A ``database@host$`` prompt: |
100 | 99 |
|
101 |
| -You could use the prompt to do a bit of database monitoring as well: |
| 100 | + .. code-block:: javascript |
102 | 101 |
|
103 |
| -.. code-block:: javascript |
| 102 | + > host = db.serverStatus().host; \\ since host should not change |
| 103 | + > prompt = function() { |
| 104 | + ... return db+"@"+host+"$ "; |
| 105 | + ... } |
| 106 | + |
| 107 | + switched to db monkeys |
| 108 | + |
| 109 | + |
| 110 | +.. example:: |
| 111 | + |
| 112 | + A prompt that also performs database monitoring: |
104 | 113 |
|
105 |
| - > prompt = function() { |
106 |
| - ... return "Uptime:"+db.serverStatus().uptime+" Files:"+db.stats().objects+" > "; |
107 |
| - ... } |
108 |
| - Uptime:5897 Files:6 > db.monkeys.save({name : "James"}); |
109 |
| - Uptime:5948 Files:7 > |
| 114 | + .. code-block:: javascript |
110 | 115 |
|
| 116 | + > prompt = function() { |
| 117 | + ... return "Uptime:"+db.serverStatus().uptime+" Files:"+db.stats().objects+" > "; |
| 118 | + ... } |
| 119 | + Uptime:5897 Files:6 > db.monkeys.save({name : "James"}); |
| 120 | + Uptime:5948 Files:7 > |
111 | 121 |
|
112 |
| -Using a real editor |
| 122 | +Using a Real Editor |
113 | 123 | -------------------
|
114 | 124 |
|
115 |
| -MongoDB 2.1+ includes the ability to use an external editor. Just run ``edit nameOfVariableOrFunction`` and the MongoDB shell will open whatever editor you have defined in your $EDITOR environment variable. When you are finished editing simply save and exit (:wq in Vim). If you wish to discard your changes, you can either not save or make your editor exit with an error (:cq in Vim or (kill-emacs 1) in Emacs). |
| 125 | +MongoDB version 2.1+ includes the ability to use an external editor. To |
| 126 | +do so, run ``edit nameOfVariableOrFunction`` and the MongoDB shell will |
| 127 | +open whatever editor you have defined in your ``$EDITOR`` environment |
| 128 | +variable. When you are finished editing, simply save and exit (``:wq`` |
| 129 | +in Vim). If you wish to discard your changes, you can either not save or |
| 130 | +make your editor exit with an error (``:cq`` in Vim or ``kill-emacs 1`` |
| 131 | +in Emacs). |
116 | 132 |
|
117 | 133 | .. code-block:: bash
|
118 | 134 |
|
119 |
| - $ EDITOR=vim mongo --nodb |
| 135 | + $ EDITOR=vim mongo --nodb |
120 | 136 |
|
121 | 137 | .. code-block:: javascript
|
122 | 138 |
|
123 |
| - MongoDB shell version: 2.1.0 |
124 |
| - > function f() {} |
125 |
| - > edit f |
126 |
| - > f |
127 |
| - function f() { |
128 |
| - print("this really works"); |
129 |
| - } |
130 |
| - > f() |
131 |
| - this really works |
132 |
| - > o = {} |
133 |
| - { } |
134 |
| - > edit o |
135 |
| - > o |
136 |
| - { "soDoes" : "this" } |
137 |
| - > |
138 |
| - |
139 |
| -.. note:: |
140 |
| - It is possible that the code in functions will be slightly modified by the JavaScript compiler when you try to edit it again. For example it may convert 1+1 in to 2 and strip out comments. The actual changes will vary based on the version of JavaScript used, but should not effect the semantics of the code, only its appearance. |
| 139 | + MongoDB shell version: 2.1.0 |
| 140 | + > function f() {} |
| 141 | + > edit f |
| 142 | + > f |
| 143 | + function f() { |
| 144 | + print("this really works"); |
| 145 | + } |
| 146 | + > f() |
| 147 | + this really works |
| 148 | + > o = {} |
| 149 | + { } |
| 150 | + > edit o |
| 151 | + > o |
| 152 | + { "soDoes" : "this" } |
| 153 | + > |
| 154 | + |
| 155 | +.. note:: It is possible that the code in functions will be slightly |
| 156 | + modified by the JavaScript compiler when you try to edit it again. |
| 157 | + For example, it may convert ``1+1`` to ``2`` or strip out comments. |
| 158 | + The actual changes will vary based on the version of JavaScript used |
| 159 | + but should not affect the semantics of the code, only the appearance. |
0 commit comments