您的位置:首页 > 数据库 > Mongodb

mongodb使用简单总结

2016-12-10 16:34 288 查看
1.mongodb特性

1)mongo是一个面向文档的数据库,它集合了nosql和sql数据库两方面的特性。

2)所有实体都是在首次使用时创建。

3)没有严格的事务特性,但是它保证任何一次数据变更都是原子性的。

4)也没有固定的数据模型

5)mongo以javascript作为命令行执行引擎,所以利用shell进行复杂的计算和查询时会相当的慢。

6)mongo本身支持集群和数据分片

7)mongo是c++实现的,支持windows mac linux等主流操作系统

8)性能优越,速度快

2.mongo常用操作

1.增删操作
   db.user.insert({name:'aaaa',age:30});
   db.user.save({name:'aaaa',age:30});
   db.collection.insertOne({});(3.2新特性)
   db.collection.deleteOne(<filter>,{});(3.2新特性)
   db.collection.remove({name:'aaa'});
   db.collection.remove();(删除全部)
  

2.更新操作

  db.users.update ({   " name"   :   "joe"   },   joe );
  db.users.update ({   " name"   :   "joe"   },   joe,  true );------upsert模式
  db.users.update ({   " name"   :   "joe"   },   joe,  true ,true);------MULTI模式

update是对文档替换,而不是局部修改默认情况update更新匹配的第一条文档,multi模式更新所有匹配的

3.查询操作

  -- 普通查询

  db.user.find();
  db.user.find({name:'aaa'});
  db.user.findOne({name:'aaa'});

  -- 模糊查询

  db.UserInfo.find({userName :/A/}) (名称%A%)
  db.UserInfo.find({userName :/^A/}) (名称A%)

4.操作符

    1.$lt, $lte,$gt, $gte(<, <=, >, >= )     

    2.$all    数组中的元素是否完全匹配  db.things.find( { a: { $all: [ 2, 3 ] } } );

    3.$exists  可选:true,false  db.things.find( { a : { $exists : true } } );

    4.$mod  取模:a % 10 == 1  db.things.find( { a : { $mod : [ 10 , 1 ] } } );

    5.$ne 取反:即not equals  db.things.find( { x : { $ne : 3 } } );

    6.$in 类似于SQL的IN操作  db.things.find({j:{$in: [2,4,6]}});

    7.$nin $in的反操作,即SQL的  NOT IN  db.things.find({j:{$nin: [2,4,6]}});

    8.$nor $or的反操作,即不匹配(a或b)  db.things.find( { name : "bob", $nor : [ { a : 1 },{ b : 2 }]})

    9.$or Or子句,注意$or不能嵌套使用  db.things.find( { name : "bob" , $or : [ { a : 1 },{ b : 2 }]})

    10.$size  匹配数组长度  db.things.find( { a : { $size: 1 } } );

    11.$type  匹配子键的数据类型,详情请看  db.things.find( { a : { $type : 2 } } );

5.数组查询

    $size 用来匹配数组长度(即最大下标)  

    // 返回comments包含5个元素的文档   

    db.posts.find({}, {comments:{‘$size': 5}});  

    // 使用冗余字段来实现  

    db.posts.find({}, {‘commentCount': { ‘$gt': 5 }});   

    $slice 操作符类似于子键筛选,只不过它筛选的是数组中的项  

    // 仅返回数组中的前5项  

    db.posts.find({}, {comments:{‘$slice': 5}});  

    // 仅返回数组中的最后5项  

    db.posts.find({}, {comments:{‘$slice': -5}});  

    // 跳过数组中的前20项,返回接下来的10项  

    db.posts.find({}, {comments:{‘$slice': [20, 10]}});  

    // 跳过数组中的最后20项,返回接下来的10项  

    db.posts.find({}, {comments:{‘$slice': [-20, 10]}});  

    MongoDB 允许在查询中指定数组的下标,以实现更加精确的匹配  

    // 返回comments中第1项的by子键为Abe的所有文档  

    db.posts.find( { "comments.0.by" : "Abe" } );   

3.索引的使用

1.创建索引

    db.things.ensureIndex ({'j': 1});

    创建子文档 索引

    db.things.ensureIndex ({'user.Name' : - 1});

    创建 复合 索引

    db.things.ensureIndex ({

    'j' : 1 ,   //  升序

    'x' : - 1   //  降序

    });

    如果 您的 find 操作只用到了一个键,那么索引方向是无关紧要的  

    当创建复合索引的时候,一定要谨慎斟酌每个键的排序方向

2.修改索引

    修改索引,只需要重新 运行索引 命令即可  

    如果索引已经存在则会 重建, 不存在的索引会被 添加  

    db . things . ensureIndex ({

        --- 原来的索引会 重建

        'user.Name ' :   - 1 ,

        --- 新增一个升序 索引

        'user.Name ' :   1 ,

        ---  为 Age 新建降序 索引

        'user.Age ' :   - 1

    }, 

    打开后台执行

    {    ‘background' :   true} 

    );

    重建索引

    db. things .reIndex();    

3.删除索引

    删除集合中的所有 索引

    db . things . dropIndexes ();  

    删除指定键的索引  

    db.things.dropIndex ({

        x :   1 ,

        y :   - 1

    });  

    使用 command 删除指定键的 索引

    db.runCommand ({

        dropIndexes : 'foo ' ,

        index  :   {   y :   1   }

    });  

    使用 command 删除所有 索引

    db . runCommand ({dropIndexes : 'foo ' ,index  :   '*‘})

    如果是删除集合中所有的文档(remove)则不会影响索引,当有新文档插入时,索引就会重建。 

4.唯一索引

    创建唯一索引,同时这也是一个符合唯一索引  

    db.things.ensureIndex (

    {

        'firstName ' :   1 ,

        'lastName ' :   1

    },   {

    指定为唯一索引

    'unique ' :   true ,

    删除重复 记录

    'dropDups ' :   true

    });

5、强制使用索引

  强制使用索引 a 和 b 

    db.collection.find ({

        'a ' :   4 ,

        'b ' :   5 ,

        'c ' :   6

    }). hint ({

        'a ' :   1 ,

        'b ' :   1

    });

    强制不使用任何 索引

    db.collection.find ().hint ({

        '$ natural' :   1

    });

索引总结:
    索引可以加速查询;
    单个索引无需在意其索引方向;
    多键索引需要慎重考虑每个索引的方向;
    做海量数据更新时应当先卸载所有索引,待数据更新完成后再重建索引;
    不要试图为每个键都创建索引,应考虑实际需要,并不是索引越多越好;
    唯一索引可以用来消除重复记录;
    地理空间索引是没有单位的,其内部实现是基本的勾股定理算法

文章转自:http://www.jb51.net/article/78928.htm  在此鸣谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb