mongoDB笔记2-shell Database对象

#mongoDB shell下database对象的方法

在shell下,指令db.help()将列出database的所有可见方法(版本不同,方法略有不同,比如addUser就一直在变化,然后到3.0变没了,多了个createUser,嗯目前学习的是3.0.2)

此部分的官方手册:传送门

 

#但是貌似直接官方的文档跟实际的函数不是很一致

e.g. 

>db.runCommand
function ( 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.js
MongoDB shell version: 3.0.2
connecting to: test
demo(1):
arg1 is:1
arg2 is:undefined

demo(1,2,3)
arg1 is:1
arg2 is:2

demo(1,2)
arg1 is:1
arg2 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什么的...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值