同mysql数据库类似,mongoDB也可通过mongo客户端连接到mongod服务器来进行绝大多数日常管理。这个命令行工具就是mongo,在mysql中则是mysql。通过mongo命令可以连接到本机,异机,以及在linux shell或者mongo shell下执行js脚本。本文即是对此展开的描述。
一、mongo客户端连接到mongoDB
//mongo连接格式
$ mongo some-host:30000/myDB
//连接到本机缺省端口,缺省数据库test
C:\Users\Think>mongo
MongoDB shell version: 3.2.9
connecting to: test
//连接到远程主机副本集,并查看版本
C:\Users\Think>mongo 192.168.1.242:27000
MongoDB shell version: 3.2.9
connecting to: 192.168.1.242:27000/test
repSetTest:PRIMARY> db.version()
3.0.12
//连接到远程主机mongos
C:\Users\Think>mongo 192.168.1.242:27017
MongoDB shell version: 3.2.9
connecting to: 192.168.1.242:27017/test
mongos>
//连接到特定的DB,端口号后加"/"及DB名
C:\Users\Think>mongo 192.168.1.242:27000/tempdb
MongoDB shell version: 3.2.9
connecting to: 192.168.1.242:27000/tempdb
repSetTest:PRIMARY> db
tempdb
//不连接任何DB,然后在提示符下进行连接DB
C:\Users\Think>mongo --nodb
MongoDB shell version: 3.2.9
> db
2016-09-06T14:56:19.323+0800 E QUERY [thread1] ReferenceError: db is not defined :
@(shell):1:1
> conn = new Mongo("192.168.1.242:27000")
connection to 192.168.1.242:27000
> db = conn.getDB("tempdb")
tempdb
repSetTest:PRIMARY> db
tempdb
//使用connect方式连接数据库
C:\Users\Think>mongo --nodb
MongoDB shell version: 3.2.9
> var db=connect("192.168.1.242:27017/tempdb");
connecting to: 192.168.1.242:27017/tempdb
mongos> var list=db.user.find().toArray();
mongos> printjson(list);
[
{
"_id" : ObjectId("57cf79a7ed46066de447aac4"),
"ename" : "robin"
},
{
"_id" : ObjectId("57cf79aded46066de447aac5"),
"ename" : "jerry"
},
{
"_id" : ObjectId("57cf79b3ed46066de447aac6"),
"ename" : "fred"
}
]
二、shell提示符下执行js脚本
1、调用单个js脚本
# more find.js
var item=db.user.find({}).toArray();
printjson(item);
# mongo localhost:27000/tempdb find.js //调用时指定了主机及库名
MongoDB shell version: 3.0.12
connecting to: localhost:27000/tempdb
[
{
"_id" : ObjectId("57ce64c4843451f0d789a05e"),
"ename" : "robin"
},
{
"_id" : ObjectId("57ce6a940b4ef68464e67f8d"),
"ename" : "fred"
}
]
2、同时调用多个js脚本,如下,先执行insert.js,然后再执行find.js
# mongo localhost:27000/tempdb insert.js find.js
MongoDB shell version: 3.0.12
connecting to: localhost:27000/tempdb
loading file: insert.js
loading file: find.js
[
{
"_id" : ObjectId("57ce64c4843451f0d789a05e"),
"ename" : "robin"
},
{
"_id" : ObjectId("57ce6a940b4ef68464e67f8d"),
"ename" : "fred"
},
{
"_id" : ObjectId("57ce6c6aff7edaac9160fe47"),
"ename" : "jack"
}
]
//下面在Windows环境下调用js脚本访问Linux下的mongodb
//命令行下调用时,此时未指定主机及库名,主机及库名在js脚本中定义
C:\Users\Think>type d:\temp\mongo_test.js
var mongo=new Mongo("192.168.1.242:27017");
var db=mongo.getDB("tempdb");
var collection=db.getCollection("user");
var list= collection.find().toArray();
printjson(list);
C:\Users\Think>mongo "d:\temp\mongo_test.js"
MongoDB shell version: 3.2.9
connecting to: test
Cannot use 'commands' readMode, degrading to 'legacy' mode
[
{
"_id" : ObjectId("57cf79a7ed46066de447aac4"),
"ename" : "robin" //Author: Leshami
}, //Blog : http://blog.csdn.net/leshami
{
"_id" : ObjectId("57cf79aded46066de447aac5"),
"ename" : "jerry"
},
{
"_id" : ObjectId("57cf79b3ed46066de447aac6"),
"ename" : "fred"
}
]
3、mongo提示符下执行使用load调用js
[root@node1 ~]# mongo localhost:27000
MongoDB shell version: 3.0.12
connecting to: localhost:27000/test
repSetTest:PRIMARY> use tempdb
switched to db tempdb
repSetTest:PRIMARY> load("find.js")
[
{
"_id" : ObjectId("57ce64c4843451f0d789a05e"),
"ename" : "robin"
},
{
"_id" : ObjectId("57ce6a940b4ef68464e67f8d"),
"ename" : "fred"
},
{
"_id" : ObjectId("57ce6c6aff7edaac9160fe47"),
"ename" : "jack"
}
]
true
4、命令提示符下直接执行javascript方法
C:\Users\Think>mongo 192.168.1.242:27017/tempdb --eval "printjson(db.getCollectionNames())"
MongoDB shell version: 3.2.9
connecting to: 192.168.1.242:27017/tempdb
[ "system.indexes", "user" ]
5、mongo提示符下调用系统命令
mongos> run("ls","-l","/home/robin")
2016-09-07T09:38:21.907+0800 I - shell: started program (sh70522): ls -l /home/robin
sh70522| total 4
sh70522| -rw-r--r-- 1 root root 2 Sep 7 09:38 test.txt
0