`
leonzhx
  • 浏览: 768207 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Advanced usage of MongoDB JavaScript shell

阅读更多

 

1.   If a line contains open '(' or '{' characters, the shell will request more input before evaluating. You can press Enter twice to terminate line entry.

 

2.   Key Shortcuts:

  a)   up/down array for command history

  b)   in v1.9+ some basic emacs keystrokes work

  c)   ctrl-l to clear the screen

  d)   tab for auto-complete (newer versions only)

  e)   ctrl-c to exit

  f)   Enter twice to break out of line continuation mode

 

3.   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:

 

> cmdCount = 1;

> prompt = function() {

... return (cmdCount++) + "> ";

... }

1> command

2> anothercommand

3> 
 

 

4.   We've added a feature to allow you edit larger values including functions using your editor of choice. Just run edit nameOfVariableOrFunction and we will open whatever editor you have defined in your EDITOR environment variable. Make sure that you save the file when editing. 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).

 

5.  B y default, the shell treats all numbers as floating-point values. You have the option to work with 64 bit integers by using a class built into the shell called NumberLong() .

 

6.   The Date() function returns a string and a "new Date() " will return an object (which is what you should use to store values).

 

7.   The BSON BinData datatype is represented via class BinData in the shell. Run help misc for more information.

 

8.   The MongoDB shell is not just an interactive shell, it can also be scripted using JS files:

./mongo server:27017/dbname --quiet my_commands.js
 

 

This command will execute the my_commands.js as if it had been entered into the shell directly.

 

9.   --quiet is a flag for the mongo command. This switch removes some header information that is not typically necessary when building unattended scripts.

 

10.   In addition to using a full Javascript file you can also pass in a Javascript fragment:

./mongo test --eval "printjson(db.getCollectionNames())"

 

 

 

11.   use dbname does not work in scripted mode. Instead you will need to explicitly define the database in the connection. Alternately, you can also create a connection within the script. Or you can use:

> db = db.getSiblingDB("otherdb")
 

 

12. The iterator command it does not work outside of the interactive scripting environment.

 

13. When running an update/insert command from the shell, the shell automatically awaits a reply (i.e. runs a get last error ). The same is not true when running from a script file. To wait for the status of an operation (such as a write), run the getLastError function after update/insert.

分享到:
评论
1 楼 hill124 2013-05-13  
牛人啊,看了这么多书,文章都是English,好奇博主是怎样的人物

相关推荐

Global site tag (gtag.js) - Google Analytics