Four ways to run js scripts in mongodb shell



1. Interactive mongo shell

Most of the mongodb tutorials will explain this approach in the first chapter.
mongo 127.0.0.1:27017
use test
db.users.findOne()

2. mongo --eval run a script

Without entering the interactive mode, run a mongodb script directly on the OS command line.

mongo 127.0.0.1:27017/test --eval "printjson(db.users.findOne())"

3. On the OS command line, run a js file

mongo 127.0.0.1:27017/test userfindone.js

Contents of userfindone.js:
printjson(db.users.findOne());

4. In the mongo shell interactive mode, run a js file

mongo test
load("/root/mongojs/userfindone.js")

The file path in the load() parameter can be either a relative path or an absolute path.
The method to view the current working path under the mongo shell: pwd( )

The current working path is the path where the current user is when we start the mongo shell.
E.g:
[root@cgl-centos-dev mongojs]# pwd
/root/mongojs
[root@cgl-centos-dev mongojs]# mongo
MongoDB shell version: 2.6.12
connecting to: test
> pwd()
/root/mongojs
>

Establish database connection in js file

All of the above examples are directly connected to the database (127.0.0.1:27017/test) when running the mongo command. We can also establish database connection in js script, the third method above can be written like this:
userfindone.js file content:
conn = new Mongo("127.0.0.1:27017");
db = conn.getDB("test");
printjson(db.users.findOne());

Run at the command line:
mongo --nodb userfindone.js


Notice:
For all the above commands, if the connected database is 127.0.0.1:27017 , the host and port can be omitted, for example:
mongo test --eval "printjson(db.users.findOne())"

include other js files in the js file

Method: Use the load command described in the fourth method above.

Principle: Most mongo shell commands can be used in js files, and the load command is no exception.

E.g:

[javascript]  view plain copy  
  1. //utils.js  
  2. function sum(a,b) {  
  3.     return a + b;  
  4. }  

[javascript]  view plain copy  
  1. //dosomething.js  
  2.   
  3. load("utils.js");  
  4.   
  5. var  a = 17;  
  6. var  b = 48;  
  7. sum(a+b);  
1. Interactive mongo shell

大部分的 mongodb 教程,在第一章都会讲解这种方式。
mongo 127.0.0.1:27017
use test
db.users.findOne()

2. mongo --eval 运行一段脚本

不进入交互模式,直接在 OS 的命令行下运行一段mongodb脚本。

mongo 127.0.0.1:27017/test --eval "printjson(db.users.findOne())"

3. 在OS命令行下,运行一个js文件

mongo 127.0.0.1:27017/test userfindone.js

userfindone.js 的内容:
printjson(db.users.findOne());

4. 在mongo shell 交互模式下,运行一个js文件

mongo test
load("/root/mongojs/userfindone.js")

load() 参数中的文件路径,既可以是相对路径,也可以是绝对路径。
在mongo shell下查看当前工作路径的方法: pwd( )

当前工作路径就是我们启动mongo shell时,当前用户所处的路径。
例如:
[root@cgl-centos-dev mongojs]# pwd
/root/mongojs
[root@cgl-centos-dev mongojs]# mongo
MongoDB shell version: 2.6.12
connecting to: test
> pwd()
/root/mongojs
>

在js文件中建立数据库连接

上面所有的例子,都是在运行mongo命令时,直接连接数据库 (127.0.0.1:27017/test)。 我们也可以在js脚本中建立数据库连接,上面的第三种方法可以这么写:
userfindone.js 文件内容:
conn = new Mongo("127.0.0.1:27017");
db = conn.getDB("test");
printjson(db.users.findOne());

在命令行下运行:
mongo --nodb userfindone.js


注意:
以上所有命令,如果连接的数据库是 127.0.0.1:27017 ,则,主机和端口可以省略,例如:
mongo test --eval "printjson(db.users.findOne())"

在js文件中include其他js文件

方法:使用上面第四种方法介绍的load命令。

原理:大部分的mongo shell命令都可以在js文件中使用,load命令也不例外。

例如:

[javascript]  view plain  copy
  1. //utils.js  
  2. function sum(a,b) {  
  3.     return a + b;  
  4. }  

[javascript]  view plain  copy
  1. //dosomething.js  
  2.   
  3. load("utils.js");  
  4.   
  5. var a = 17;  
  6. var b = 48;  
  7. sum(a+b);  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173594&siteId=291194637