简介
MongoDBShell = JavaScirpt解释器 + MongoDB客户端
MongoDB Shell 是 MongoDB 自带的,使用google v8引擎实现的 javascript Shell,随MongoDB—同发布, 它是MonoDB客户端工具,可以在Shell中使用命令与MongoDB实例交互,对数据库的管理操作(CURD、集群配置、状态查看等)都可以通过MongoDBShell来完成。
基本功能
执行JavaScript命令
1 > "hello, world".replace("world", "MongoDB") hello, MongoDB 2 > function factorial(n) { ... if(n <= 1) return 1; ... return factorial(n-1) * n; ... } 3 > factorial(10) 3628800
MongoDB客户端一基本命令
- 连接/切换数据库 -- use dbname;
- 数据插入 -- db.stu.insert(obj);
- 数据查询 -- db.stu.find(query);
- 数据更新 -- db.stu.update(query,obj);
- 数据删除 -- db.stu.remove(query);
使用技巧
help查看帮助
7 > help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell
执行脚本
直接运行 例如:mongo[--quiet]script.js
交互式运行 例如:load("script.js")
准备一个js文档testMongoShell.jg:
db = connect("localhost:27017"); // 连接数据库 db = db.getSiblngDB("test"); // 选择数据库 cursor = db.demo.find(); // 查询集合 while(cursor.hasNext()) // 迭代输出结果 { printjson( cursor.next() ) }
直接运行
# (anaconda3) whoami at iamwho in [~] $ mongo --quiet testMongoShell.js { "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 } { "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 } { "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) } # (anaconda3) whoami at iamwho in [~] $ mongo testMongoShell.js MongoDB shell version v3.4.10 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.10 connecting to: localhost:27017 MongoDB server version: 3.4.10 { "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 } { "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 } { "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) }
交互式运行
1 > load("testMongoShell.js") connecting to: localhost:27017 MongoDB server version: 3.4.10 { "_id" : ObjectId("5a7863305640374fb2cd5620"), "y" : 10 } { "_id" : ObjectId("5a78636d5640374fb2cd5621"), "y" : 10 } { "_id" : ObjectId("5a7863915640374fb2cd5622"), "y" : NumberLong(10) } true
执行命令行程序
例如:run("ls")
2 > run("ls") 2018-02-05T23:15:05.650+0800 I - [thread1] shell: started program (sh5361): /bin/ls sh5361| 1.json sh5361| AnacondaProjects sh5361| avimrc 3 > run("cd avimrc") 2018-02-05T23:15:27.155+0800 I - [thread1] shell: started program (sh5366): cd avimrc sh5366| Unable to start program cd avimrc: No such file or directory 255
似乎也只能运行内置的shell命令,但是也有着很多的限制.
.mongorc.js 文件
mongo启动配置文件
- 文件分为Global和Local,前者在/etc/.mongorc.js,后者在~下面
- 可以配置命令提示符,具体参考这里
编辑复合变量EDITOR
这个我忽略了,直接在shell配置文件中export一个EDITOR不就行了吗
在shell中使用
edit + fileName or doc;
即可更多命令可参考