mongoDB笔记2-shell Database对象
来源:互联网 发布:淘宝有访客无询单 编辑:程序博客网 时间:2024/06/05 22:39
#mongoDB shell下database对象的方法
在shell下,指令db.help()将列出database的所有可见方法(版本不同,方法略有不同,比如addUser就一直在变化,然后到3.0变没了,多了个createUser,嗯目前学习的是3.0.2)
此部分的官方手册:传送门
#但是貌似直接官方的文档跟实际的函数不是很一致
e.g.
>db.runCommandfunction ( obj,extra ){ if ( typeof( obj ) == "string" ){ var n = {}; n[obj] = 1; obj = n; if ( extra && typeof( extra ) =="object" ) { for ( var x in extra ){ n[x] = extra[x]; } } } return this.getCollection( "$cmd" ).findOne( obj ); #这个应该是真正的runCommand}
官方文档中
db.runCommand(command) command可以是一个document(看来要习惯josn的另一个叫法了)
从function(obj,extra)的执行效果来看,
如果obj是一个字符串,则会把obj和extra合为一个document,而且要求extra是一个document否则将无视extra
如果obj是一个document,无视extra
╮(╯▽╰)╭ 总之官方文档确实没有错,只不过方法的实现却是比介绍更完全
#根据上边的问题,插点函数传值的测试
file: test.js
function demo(arg1,arg2){ if(arg1)print("arg1:"+arg1); if(arg2)print("arg2:"+arg2); print("\n");}print("demo(1):");demo(1);print("demo(1,2,3)");demo(1,2,3);print('demo(1,2)');demo(1,2);
执行结果:
>mongo test.jsMongoDB shell version: 3.0.2connecting to: testdemo(1):arg1 is:1arg2 is:undefineddemo(1,2,3)arg1 is:1arg2 is:2demo(1,2)arg1 is:1arg2 is:2
函数参数直接按照顺序赋值,多的丢掉,少的就是undefined
注:这是javascript的特性,mongo shell虽然跟浏览器,nodejs的解释器有不同的地方,但是函数传值这块却是一样的(已测试)
#DB methods: (版本3.0.2)
mongo shell下db.help()会给出方法列表
> db.help()
DB methods:
db.adminCommand(nameOrDocument)
#如上所言,参数数量略有变化但是可以无视
#切换到admin用户,并运行runCommand(obj,extra)
#(即useadmin;runCommand(obj,extra)
db.auth(username,password)
#换用户
db.cloneDatabase(fromhost)
#从fromhost处'克隆'一个数据库过来
#e.g.
>db.cloneDatabase("127.0.0.1:27017")
{
"clonedColls" : [ ],
"ok" : 0,
"errmsg" : "can'tclone from self (localhost)."
}
#报错是因为自己不能克隆自己;)
db.commandHelp(name)
#返回指令的帮助信息, name可以是比如 insert,delete,count...
db.copyDatabase(fromdb,todb, fromhost)
#从fromhost(可选,默认是selfhost)的fromdb拷贝一份到todb
#如果那个fromhost设置了访问权限,恩不要紧,这个函数后边其实还有俩可选参数,username,password
#恩其实后边还有一个参数AuthenticationMechanism
#但是作为初学的....恩,Mechanism以后见~
db.createCollection(name, { size : ..., capped : ..., max : ... } )
#在当前db下创建新的collection,第二个参数可以省略
#第二个参数是新建collection的设定值
db.createUser(userDocument)
#跟以前版本的addUser差不多 换什么名字= =
#userDocument是一个document:{user,pwd,customData,roles}
#后边还有一个可选参数writeConcern 指定 安全写级别(来自中文文档) 的等级
db.currentOp()
#显示当前数据库正在进行的操作
db.dropDatabase()
#删掉当前数据库
db.eval(func,args) #(函数,参数列表)
#传给服务端一个函数让服务端去运行,好强大,还有全局写权限
#官方文档给此函数的标签:Deprecated(是处于安全性的考量么)
db.fsyncLock() #权限 admin
#把数据(修改的插入的删除的)写入磁盘,并且上锁(期间将拒绝写操作)
db.fsyncUnlock() #权限 admin
#解锁
db.getCollection(cname) same as db['cname'] or db.cname
#获得当前数据库下的名为cname的collection
#如果不存在,那就暂时创建一个,如果这个collection没有插入document,就不会真正创建
db.getCollectionInfos()
#返回当前数据库的所有collection及其设置(options)信息
db.getCollectionNames()
#数组形式返回所有collections的名字
db.getLastError()
#返回上次的错误信息(string),所以返回null不一定标识没有过错误
db.getLastErrorObj() - return full status object
#返回上次的错误信息对象,比getLastError完整
db.getLogComponents()
#列出当前的各个log组件
db.getMongo()
#返回当前连接mongoDB的对象
db.getMongo().setSlaveOk()
#这个算是连接对象的方法
#允许非master的数据库实例的读操作(数据库集群..呢)
db.getName()
#活取当前数据库的名字
db.getPrevError() #类似getLastErrorObj()
#自版本1.6就是 Deprecated,竟然保留到了3.0
db.getProfilingLevel() - deprecated
#获得profileLevel (0~2)
db.getProfilingStatus()- returns if profiling is on and slow threshold
#获得profile状态,包括level和slowms
# slowms 用来界定slow operations,然后默认mongoDB会将slow operations写入log
db.getReplicationInfo()
#返回the replica set(集群)的状态信息
db.getSiblingDB(name)
#返回一个数据库对象,但是并不像user databasename那样改变shell里边的全局对象db
db.getWriteConcern()
#返回当前数据库的安全写级别
db.hostInfo() get details about the server's host
#获取当前计算机的一些信息比如操作系统,内存,cpu参数等
db.isMaster()
#返回一个document,描述当前数据库实例的角色(相关:数据库集群)
db.killOp(opid) kills the current operation in the db
#结束id为opid的operation
db.listCommands() lists all the db commands
#列出所有的数据库cmmmand
db.loadServerScripts() loads all the scripts in db.system.js
#载入db.system.js中的所有scripts
db.logout()
#登出
db.printCollectionStats()
#显示collections的状态
db.printReplicationInfo()
#打印一个格式化的报告of the replica set member's oplog.
db.printShardingStatus()
# = =,文档,
db.printSlaveReplicationInfo()
#相关:数据库集群,文档
db.dropUser(username)
#删掉用户
db.repairDatabase()
#检查并修复数据存储中的错误和不一致情况
db.resetError()
#重置错误信息(这个错误信息可由db.getPrevError()返回)
db.runCommand(cmdObj)
#见上文
db.serverStatus()
#当前连接的mongoDB数据库的状态
db.setLogLevel(level,<component>)
#设定某log组件的级别(0~5),越高,log越详细
db.setProfilingLevel(level,<slowms>)0=off 1=slow 2=all
#设定profile级别,level (0~2)
#profile 影响数据库的运行性能
#slowms 可选,默认100ms,
db.setWriteConcern(<write concern doc> ) - sets the write concern for writes to the db
#为当前数据库设定安全写级别
db.unsetWriteConcern( <write concern doc> ) - unsets the write concernfor writes to the db
#取消(移除) 当前数据库的安全写级别
db.setVerboseShell(flag)# display extra information in shell output
#在shell中输出更多额外信息
#虽然db.help()中有输出,但是手册中没有呃。。而且还没有这个方法(method),版本问题么。
db.shutdownServer()
#关闭当前连接分mongoDB数据库
db.stats()
#当前数据库状态
db.version()
#版本号 e.g. 3.0.2
#果然初学不要贪多=_=#,集群,write concern,profile什么的...
- mongoDB笔记2-shell Database对象
- MongoDB笔记3-shell collection对象
- Mongodb shell语法笔记
- mongoDB学习笔记--shell基本语法(二)
- mongoDB(2):shell基本操作
- MongoDB: Database Profiler
- MongoDB: Database Profiler
- MongoDB: Database Profiler
- mongodb DataBase与Collection
- mongodb-shell
- MongoDB Shell
- mongodb shell
- MongoDB Shell
- mongodb shell
- MongoDB 学习笔记2
- MongoDb学习笔记-2
- shutdown& startup database shell
- MongoDB 学习笔记(一):安装及简单shell操作
- spark:学习杂记+如何求丢失的数据例子--25
- 李白喝酒问题
- Android开发——Intent(2)
- 黑马程序员——C语言基础篇---指针
- 自定义封装简单的基于FMDB以及数据model的数据库操作
- mongoDB笔记2-shell Database对象
- poj2549
- Linux集群Hadoop2.5.1完全分布式安装
- 趣味CRACKME破解分析(发散思维)
- 网络之协议
- Android 开源框架Universal-Image-Loader完全解析(三)---可以防止图片错乱
- linux中sparce与__be32
- java学习入门
- [CS231n@Stanford] Assignment1-Q1