MongoDB笔记3-shell collection对象

来源:互联网 发布:龙族js挂机脚本 编辑:程序博客网 时间:2024/06/05 15:47
#mongoDB shell 中collection对象的方法
手册:传送门
版本:3.0.2

#method list:
>db.getCollection("coll").help()
DBCollection help #db.coll是数据库下一个名为coll的collection
        db.coll.find().help() - show DBCursor help
               #显示DBCursor的帮助信息(方法列表)

        db.coll.count()
               #返回此collection下document的个数

        db.coll.copyTo(newColl)
               #将当前collection拷贝一份到当前数据库下的名为newColl的collection,但是建立的索引不会拷贝

        db.coll.convertToCapped(maxBytes)
               #调用 {convertToCapped:'coll', size:maxBytes}}命令,把当前collection转成capped collection,并将其maxSize更新为maxBytes
               #如果maxBytes不是4096的倍数,那么将采用进一法convertToCapped(4097)等价于convertToCapped(4096*2)   --关于这个手册没写,是规律(什么鬼=_=#)
               #capped collection:固定大小的collection,当插入的document超过设定的大小时,会自动覆盖最早插入的document
               #(once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.)
               #maxBytes是指定的大小,单位应该是byte吧,看手册collStats.size
               
        db.coll.dataSize()
               #返回当前collection的大小,(随数据插入而变大)
          
        db.coll.distinct( key ) - e.g. db.coll.distinct( 'x' )
               #遍历当前collection下的所有document的名为key的field的值,然后返回一个这些值的数组(去重)              

        db.coll.drop()
               #删除当前collection

        db.coll.dropIndex(index) - e.g. db.coll.dropIndex( "indexName" ) or db.coll.dropIndex( { "indexK
ey" : 1 } )
               #删除根据索引名字删除索引

        db.coll.dropIndexes()
               #删除所有索引

        db.coll.ensureIndex(keypattern[,options])
                #从版本3.0开始,Deprecated  ,然后同createIndex
                #如果在指定的field(document:{field:value,field:value,...})没有建立索引,那就建立

        db.coll.explain().help() - show explain help
               #explain对象的help
     
        db.coll.reIndex()
               #重建现存索引

        db.coll.find([query],[fields])  #关于查询看来得重点照顾下
               #类似于select fieds from..where query
               #find()  返回全部document(完整)
               #find({},{name:1,age:1})  返回全部document(_id,name,age)  如果有的document没有age,那么就不显示这个field
               #find({age:11},{})   返回documents中age值为11的
               #find({age:11},{name:1})    ,嗯,11岁的都叫啥
               #返回值是一个DBCursor,所以之后的那一堆方法其实是属于DBCursor的
               #版本3.0的find函数完整的定义是这样的function ( query , fields , limit , skip, batchSize, options )
               #手册上就是不写就是不写,这样真的好么= =

        db.coll.find(...).count()  #学到DBcursor再说吧
        db.coll.find(...).limit(n)
        db.coll.find(...).skip(n)
        db.coll.find(...).sort(...)

        db.coll.findOne([query])
               #只返回符合条件的第一条document

        db.coll.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
               #找到符合条件的第一个document并进行修改
               #接受参数是一个document,包括:query,sort,remove,update,new,fields,upsert
                    #remove和update的其中一个必须指明 remove的是true/fasle   update的值是更新的document
                    #query和sort协作来定位document
                    #new 如果执行new是true,返回值是修改之后的document,当然如果操作是删掉,返回...null吧,如果值是false,返回修改之前的document
                    #fields 返回值的fields,(同find) e.g. {}
                    #upsert  值域:true/false   估计是update 和 insert的结合
                         #默认是false,如果是true,那么如果没有符合条件的document,那么就创建一个新的并返回,如果有符合条件的,那么就更新它
                         # To avoid multiple upserts, ensure that the query fields are uniquely indexed.
                         #避免多次upserts,确保query 部分的fields是uniquely indexed

        db.coll.getDB()
              #返回当前collection所属的db对象
      
        db.coll.getPlanCache()
                # 返回一个和当前collection相关的query plan cache对象

        db.coll.getIndexes()
               #好尴尬,3.0手册上没有,在2.6手册上发现了
               #返回一个document数组来显示当前collection的索引情况
                    #每个document的结构:{v:版本号,key:{索引字段:排序方式},name:'a unique name',ns:"此索引的命名空间[nameapace ns]上下文"}       
               
        db.coll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )  
               #将此collection下的documents分组,通过指定的key值并进行一些简单的聚集函数比如count和sum
               #e.g.    db.orders.group( {
                                                       key: { ord_dt: 1, 'item.sku': 1 }, #按照ord_dt和item的sku来进行分组
                                                       cond: { ord_dt: { $gt: new Date( '01/01/2012' ) } },  #筛选ord_dt大于..的
                                                       reduce: function ( curr, result ) {result.count++; },  
                                                       finalize: function(result){  }
                                                       initial: {count:0 }   #reduce 和initial 共同进行聚集函数方面的操作
                                                       })
                         #reduce方法将对分好的每个组进行操作,上述例子完成了对每组的计数  result对应initial对象
                         #finalize方法将在每组reduce方法调用完毕之后调用一次,对initial进行最后的修改,比如求平均
                         #返回结果:key中指定的fields和inital中的field
                         #key可以用keyf替代,keyf是一个函数,返回一个document  ,与key相比,keyf是动态的
                         #必须有的参数:key/keyf ,reduce(函数体里边可以什么都不做),initial(可以是空对象)

        db.coll.insert(obj)
                #插入新的document 
                #obj可以是单个document,也可以是document的数组(插入多条)
                #手册上的insert方法:db.collection.insert(obj,options)
                    #options包含两个可选fields:  writeConcern, ordered(true/false)
                         #第一个是安全写级别,第二个如果是true(默认),就按顺序,如果其中某一条出现错误,那么之后的documents就不插入了,false将会继续插入

        db.coll.mapReduce( mapFunction , reduceFunction , <optional params> )
               #可以运行任意的聚集函数,基于当前collection下的所有documents
               #略复杂,以后研究
               #手册:传送门

        db.coll.aggregate( [pipeline], <optional params> ) 
               # performs an aggregation on a collection;
               #返回一个DBcursor,可据此遍历结果值
               #跟mapReduce的异同?

        db.coll.remove(query)
               #将匹配的documents删掉

        db.coll.renameCollection( newName , <dropTarget> ) renames the collection.
               #重命名当前collection,第二个参数默认false,如果新的名字已被用,返回{ "ok" : 0, "errmsg" : "target namespace exists" }   
               #dropTarget设定为true,那么将会在重命名之前把已经存在的那个collection删掉

        db.coll.runCommand( cmd , params ) 
               #runs a db command with the given name where the first param is the collection name
                    #如果cmd是一个对象(document),那么,直接调用db.runcommand
                    #如果是命令名字  比如 "insert",那么将其连同params打包进cmd对象中,调用db.runcommand
     
        db.coll.save(obj,writeconcern)
               #打包了insert和update  没有就插入,已经有了就更新
               #额外可选参数writeconcern 安全写级别
               
        db.coll.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsNam
e: <index name>})
               #返回关于当前collection的数据
               #参数都是可选的
                   #scale控制显示项的最大大小:单位byte
               #这个感觉基本不用,文档
               
        db.coll.storageSize() - includes free space allocated to this collection
               #返回分配给当前collection的空间(包含还未使用的部分)
     
        db.coll.totalIndexSize() - size in bytes of all the indexes
               #返回当前collection的所有索引的大小

        db.coll.totalSize() - storage allocated for all data and indexes
               #上边两个的加法

        db.coll.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an
object with fields: upsert, multi
               #更新一条或者多条数据
               #参数
                    #query 筛选documents,同find
                    #object 描述怎样更新
                              #必须仅仅包含field的更新操作
                    #upsert_bool 默认false,若为true,则如果没有符合条件的document,就创建一个
                    #muti_bool 默认false,若为true,更新所有匹配到的documents,若为false,更新第一条

        db.coll.validate( <full> ) - SLOW
               #检查当前collection,此方法扫描当前collecton的数据结构,
               #返回一个document来描述逻辑collection和实际数据存储的关系
               #full的值默认为fasle,若为true,则是一个更为完全的检查,但是这样将可能影响mongod实例的性能          

        db.coll.getShardVersion() - only for use with sharding
               #获得shard的版本(只有用了shard(这个东东跟数据库集群,分片有关, 手册)的时候才能用)
        db.coll.getShardDistribution() - prints statistics about data distribution in the cluster
               #以后用到的时候看手册吧= =,               

        db.coll.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and re
turns splitter function
               #别逗,手册上没发现此函数,嗯chunk 跟上边的sharded有关系,估计猜测和分片有关系(split)

        db.coll.getWriteConcern() 
               #返回 此collection的安全写级别,如果未设,则从所属的数据库继承
         
        db.coll.setWriteConcern( <write concern doc> ) 
     #设定安全写级别

        db.coll.unsetWriteConcern( <write concern doc> ) 
               #取消安全写级别



0 0
原创粉丝点击