分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
同mysql数据库类似,mongoDB也可通过mongo客户端连接到mongod服务器来进行绝大多数日常管理。这个命令行工具就是mongo,在mysql中则是mysql。通过mongo命令可以连接到本机,异机,以及在linux shell或者mongo shell下执行js脚本。本文即是对此展开的描述。
一、mongo客户端连接到mongoDB
//mongo连接格式$ mongo some-host:30000/myDB //连接到本机缺省端口,缺省数据库testC:\Users\Think>mongoMongoDB shell version: 3.2.9connecting to: test//连接到远程主机副本集,并查看版本C:\Users\Think>mongo 192.168.1.242:27000MongoDB shell version: 3.2.9connecting to: 192.168.1.242:27000/testrepSetTest:PRIMARY> db.version()3.0.12//连接到远程主机mongosC:\Users\Think>mongo 192.168.1.242:27017MongoDB shell version: 3.2.9connecting to: 192.168.1.242:27017/testmongos>//连接到特定的DB,端口号后加"/"及DB名C:\Users\Think>mongo 192.168.1.242:27000/tempdbMongoDB shell version: 3.2.9connecting to: 192.168.1.242:27000/tempdbrepSetTest:PRIMARY> dbtempdb//不连接任何DB,然后在提示符下进行连接DBC:\Users\Think>mongo --nodbMongoDB shell version: 3.2.9> db2016-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")tempdbrepSetTest:PRIMARY> dbtempdb//使用connect方式连接数据库C:\Users\Think>mongo --nodbMongoDB shell version: 3.2.9> var db=connect("192.168.1.242:27017/tempdb");connecting to: 192.168.1.242:27017/tempdbmongos> var list=db.user.find().toArray();mongos> printjson(list);[ { "_id" : ObjectId("57cf79a7ed46066de447aac4"), "ename" : "robin" }, { "_id" : ObjectId("57cf79aded46066de447aac5"), "ename" : "jerry" }, { "_id" : ObjectId("57cf79b3ed46066de447aac6"), "ename" : "fred" }]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
二、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.12connecting 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.12connecting to: localhost:27000/tempdbloading file: insert.jsloading 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.jsvar 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.9connecting to: testCannot 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:27000MongoDB shell version: 3.0.12connecting to: localhost:27000/testrepSetTest:PRIMARY> use tempdbswitched to db tempdbrepSetTest:PRIMARY> load("find.js")[ { "_id" : ObjectId("57ce64c4843451f0d789a05e"), "ename" : "robin" }, { "_id" : ObjectId("57ce6a940b4ef68464e67f8d"), "ename" : "fred" }, { "_id" : ObjectId("57ce6c6aff7edaac9160fe47"), "ename" : "jack" }]true4、命令提示符下直接执行javascript方法C:\Users\Think>mongo 192.168.1.242:27017/tempdb --eval "printjson(db.getCollectionNames())"MongoDB shell version: 3.2.9connecting 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/robinsh70522| total 4sh70522| -rw-r--r-- 1 root root 2 Sep 7 09:38 test.txt0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103