MongoDB 操作命令


MongoDB基本操作命令

11.id key
   系统自动生成也可以手动指定
   系统自动生成:BSON类型的,12个byte的2进制数据,
   12 个byte:4byte的时间戳timestamp,
              3-byte machine id,
              2-byte process id,
              3-byte counter
    
    索引被存储在system.indexes 集合里
    
12.sort 函数 Sort, Limit, and Skip    
    sort 对返回结果进行排序
    db.media.find().sort( { Title: 1 })
    1为升序,-1为降序 默认为升序
   
    If you specify a key for sorting that does not exist, the values will be returned in their ascending insertion order.
    limit 限制结果返回的数量
    db.media.find().limit( 10 )
   
  13.natural order,Capped Collections,$natural
       natural order 自然语序,数据在集合里按照插入的顺序,但是没有任何担保他们在集合里是完全按照插入的顺序,因为中途涉及到更改,删除
       Capped Collections  完全是按照插入的顺序排列的,有担保的,有固定的大小,一旦达到了峰值,那么最老的数据将被删除,新数据将被加到末尾
                          创建的时候必须明确指出createCollection 函数,必须带一个参数指定集合的大小 例如:
                          db.createCollection("audit", {capped:true, size:20480}){ "ok" : 1 } 创建集合audit,大小为20480
                          使用max 函数限制Capped collection 集合的 条目数量也就是mysql中的 行数
                          note:Capped Collections 集合里的文档能被更新,但是不能超过集合 大小,不能删除,可以drop整个集合
                          可使用validate() 函数查看集合剩余的条目数 也即行数
                         
       $natural           反转 capped collection 集合的顺序,db.audit.find().sort( { $natural: -1 } ).limit ( 10 )
   
  14.检索一个文档 (Retrieving a Single Document)
     findOne()
    
  15.聚合命令(Using the Aggregation Commands)  
     (1)count
     (2)distinct
     (3)group
    
      --count 返回集合的文档个数 类似于sql 语句中的count 
      --distinct 去重 unique 特性
      --group group函数有三个参数  key, initial, 和 reduce.
        (1)key 根据哪一个文档里面的属性进行分组.相当于mysql中的 列名。
        (2)initial 返回分类结果,默认为0.只代表返回所有计算的真实结果
        (3)reduce 组合相似的结果在一起,有两个额外的参数 prev 和items
       
        除了这3基本参数,还有额外的三个其他的参数 也能被使用
        In addition to the key, initial, and reduce parameters, you can specify three more optional
parameters:

keyf: You can use this parameter to replace the key parameter if you do not wish to group the
results on an existing key in your documents. Instead, you would group them using another
function you design that specifies how to do grouping.
cond: You can use this parameter to specify an additional statement that must be true before a
document will be grouped. You can use this much as you use the find() query to search for
documents in your collection. If this parameter isn’t set (the default), then all documents in the
collection will be checked.
   finalize: You can use this parameter to specify a function you want to execute before the final
results are returned. For instance, you might calculate an average or perform a count and
include this information in the results.
                   
         Note: The group() function does not currently work in sharded environments. For these, you should use the
mapreduce() function instead. Also, the resulting output cannot contain more than 10,000 keys in all with the
group() function, or an exception will be raised. This too, can be bypassed by using mapreduce().                    

16.条件查询
   $gt 大于 (不包括)
   $lt 小于 (不包括)
   $gte 大于等于
   $lte 小于等于
    > db.media.find ( { Released : {$gt : 2000} }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
> db.media.find ( { Released : {$gte : 1999 } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c43694bc603000000007ed1"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
> db.media.find ( { Released : {$lt : 1999 } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c436969c603000000007ed2"), "Type" : "DVD", "Title" : "Blade Runner",
"Released" : 1982 }
> db.media.find( {Released : {$gte: 1990, $lt : 2010}}, { "Cast" : 0 })
{ "_id" : ObjectId("4c43694bc603000000007ed1"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }

17.检索所有的文档除了被指定的
   $ne (not equals)
    > db.media.find( { Type : "Book", Author: {$ne : "Plugge, Eelco"})
18.基本操作命令
     (1)$in 相当于sql 中的in 
     > db.media.find( {Released : {$in : ["1999","2008","2009"] } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c43694bc603000000007ed1"), "Type" : "DVD", "Title" : "Matrix, The",
"Released" : 1999 }
     (2)$nin 相当于not in
     > db.media.find( {Released : {$nin : ["1999","2008","2009"] },Type : "DVD" },
{ "Cast" : 0 } )
{ "_id" : ObjectId("4c436969c603000000007ed2"), "Type" : "DVD", "Title" :
"Blade Runner", "Released" : 1982 }
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
(3)$all 必须都匹配
> db.media.find ( { Released : {$all : ["2010","2009"] } }, { "Cast" : 0 } )
(4)$or
(5)$slice 限制数组的个数 可以根据下表的范围检索 是 limit 和skip的结合函数,但是 limit和skip 不能操作数组
(6)$mod 奇数,偶数
    Released : { $mod: [2,0] }
    Released : { $mod: [2,1] }
(7)$size 匹配指定数量的数组集合  
(8)$exists  Author : {$exists : true }  Author : {$exists : false }
(9)$type
(10)$elemMatch 匹配整个数组
(11)$not 和 $elemMatch 相反
(12)javaScript 函数
     f = function() { return this.Released < 1995;}
         db.media.find(f)
     (13)正则表达式  
    
  19.更新(update) 
     update() criteria, objNew, upsert, and multi 
    
     (1)The criteria argument lets you specify the query that selects the record you want to update.
     (2)You use the objNew argument to specify the updated information;
     (3)The upsert argument lets you specify whether the update should be an upsert,An upsert argument
        tells MongoDB to update the record if it exists, and create it if it doesn’t
     (4)the multi argument lets you specify whether all matching documents should be updated or just the first one (the default action).
        Note that the multi argument only works with $ operators.  
     (5)$inc   {$inc: {"Read" : 4} } 给read +4
     (6)$set   {$set : { Genre :"Sci-Fi" }
     (7)$unset  {$unset : { "Genre" : 1 } }
     (8)$push 如果是已经存在的数组字段,则加入,如果不存在,则创建字段数组,如果存在不是数组,则报错
     (9)$pushAll 规则同$push 一样
     (10)$addToSet
     (11)$pop,$pull, $pullAll

猜你喜欢

转载自wwangcg.iteye.com/blog/1319370