国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MongoDB > 正文

mongodb使用心得簡(jiǎn)單總結(jié)

2020-10-29 18:50:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.mongodb特性

1)mongo是一個(gè)面向文檔的數(shù)據(jù)庫(kù),它集合了nosql和sql數(shù)據(jù)庫(kù)兩方面的特性。
2)所有實(shí)體都是在首次使用時(shí)創(chuàng)建。
3)沒(méi)有嚴(yán)格的事務(wù)特性,但是它保證任何一次數(shù)據(jù)變更都是原子性的。
4)也沒(méi)有固定的數(shù)據(jù)模型
5)mongo以javascript作為命令行執(zhí)行引擎,所以利用shell進(jìn)行復(fù)雜的計(jì)算和查詢(xún)時(shí)會(huì)相當(dāng)?shù)穆?br />6)mongo本身支持集群和數(shù)據(jù)分片
7)mongo是c++實(shí)現(xiàn)的,支持windows mac linux等主流操作系統(tǒng)
8)性能優(yōu)越,速度快

2.mongo常用操作

1.增刪操作

  1.    db.user.insert({name:'aaaa',age:30});
  2.    db.user.save({name:'aaaa',age:30});
  3.    db.collection.insertOne({});(3.2新特性)
  4.    db.collection.deleteOne(<filter>,{});(3.2新特性)
  5.    db.collection.remove({name:'aaa'});
  6.    db.collection.remove();(刪除全部)
  7.   

2.更新操作

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

update是對(duì)文檔替換,而不是局部修改默認(rèn)情況update更新匹配的第一條文檔,multi模式更新所有匹配的

3.查詢(xún)操作

  -- 普通查詢(xún)

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

  -- 模糊查詢(xún)

  1.   db.UserInfo.find({userName :/A/}) (名稱(chēng)%A%)
  2.   db.UserInfo.find({userName :/^A/}) (名稱(chēng)A%)

4.操作符

    1.$lt, $lte,$gt, $gte(<, <=, >, >= )    
    2.$all    數(shù)組中的元素是否完全匹配  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 類(lèi)似于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  匹配數(shù)組長(zhǎng)度  db.things.find( { a : { $size: 1 } } );
    11.$type  匹配子鍵的數(shù)據(jù)類(lèi)型,詳情請(qǐng)看  db.things.find( { a : { $type : 2 } } );

5.數(shù)組查詢(xún)

    $size 用來(lái)匹配數(shù)組長(zhǎng)度(即最大下標(biāo)) 
    // 返回comments包含5個(gè)元素的文檔  
    db.posts.find({}, {comments:{‘$size': 5}}); 
    // 使用冗余字段來(lái)實(shí)現(xiàn) 
    db.posts.find({}, {‘commentCount': { ‘$gt': 5 }});  
    $slice 操作符類(lèi)似于子鍵篩選,只不過(guò)它篩選的是數(shù)組中的項(xiàng) 
    // 僅返回?cái)?shù)組中的前5項(xiàng) 
    db.posts.find({}, {comments:{‘$slice': 5}}); 
    // 僅返回?cái)?shù)組中的最后5項(xiàng) 
    db.posts.find({}, {comments:{‘$slice': -5}}); 
    // 跳過(guò)數(shù)組中的前20項(xiàng),返回接下來(lái)的10項(xiàng) 
    db.posts.find({}, {comments:{‘$slice': [20, 10]}}); 
    // 跳過(guò)數(shù)組中的最后20項(xiàng),返回接下來(lái)的10項(xiàng) 
    db.posts.find({}, {comments:{‘$slice': [-20, 10]}}); 
    MongoDB 允許在查詢(xún)中指定數(shù)組的下標(biāo),以實(shí)現(xiàn)更加精確的匹配 
    // 返回comments中第1項(xiàng)的by子鍵為Abe的所有文檔 
    db.posts.find( { "comments.0.by" : "Abe" } );  

3.索引的使用

1.創(chuàng)建索引

    db.things.ensureIndex ({'j': 1});
    創(chuàng)建子文檔 索引
    db.things.ensureIndex ({'user.Name' : - 1});
    創(chuàng)建 復(fù)合 索引
    db.things.ensureIndex ({
    'j' : 1 ,   //  升序
    'x' : - 1   //  降序
    });
    如果 您的 find 操作只用到了一個(gè)鍵,那么索引方向是無(wú)關(guān)緊要的 
    當(dāng)創(chuàng)建復(fù)合索引的時(shí)候,一定要謹(jǐn)慎斟酌每個(gè)鍵的排序方向

2.修改索引

    修改索引,只需要重新 運(yùn)行索引 命令即可 
    如果索引已經(jīng)存在則會(huì) 重建, 不存在的索引會(huì)被 添加 
    db . things . ensureIndex ({
        --- 原來(lái)的索引會(huì) 重建
        'user.Name ' :   - 1 ,
        --- 新增一個(gè)升序 索引
        'user.Name ' :   1 ,
        ---  為 Age 新建降序 索引
        'user.Age ' :   - 1
    },
    打開(kāi)后臺(tái)執(zhí)行
    {    ‘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)則不會(huì)影響索引,當(dāng)有新文檔插入時(shí),索引就會(huì)重建。

4.唯一索引

    創(chuàng)建唯一索引,同時(shí)這也是一個(gè)符合唯一索引 
    db.things.ensureIndex (
    {
        'firstName ' :   1 ,
        'lastName ' :   1
    },   {
    指定為唯一索引
    'unique ' :   true ,
    刪除重復(fù) 記錄
    'dropDups ' :   true
    });

5、強(qiáng)制使用索引

  強(qiáng)制使用索引 a 和 b
    db.collection.find ({
        'a ' :   4 ,
        'b ' :   5 ,
        'c ' :   6
    }). hint ({
        'a ' :   1 ,
        'b ' :   1
    });
    強(qiáng)制不使用任何 索引
    db.collection.find ().hint ({
        '$ natural' :   1
    });

索引總結(jié):

  1.     索引可以加速查詢(xún);
  2.     單個(gè)索引無(wú)需在意其索引方向;
  3.     多鍵索引需要慎重考慮每個(gè)索引的方向;
  4.     做海量數(shù)據(jù)更新時(shí)應(yīng)當(dāng)先卸載所有索引,待數(shù)據(jù)更新完成后再重建索引;
  5.     不要試圖為每個(gè)鍵都創(chuàng)建索引,應(yīng)考慮實(shí)際需要,并不是索引越多越好;
  6.     唯一索引可以用來(lái)消除重復(fù)記錄;
  7.     地理空間索引是沒(méi)有單位的,其內(nèi)部實(shí)現(xiàn)是基本的勾股定理算法
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 神农架林区| 安泽县| 龙井市| 阳江市| 大埔区| 温泉县| 大埔区| 铜梁县| 全南县| 湘潭县| 拉萨市| 蓝田县| 措勤县| 通河县| 达州市| 南宫市| 芜湖市| 南城县| 濉溪县| 尉氏县| 商丘市| 东平县| 芜湖县| 藁城市| 白朗县| 湖口县| 宝应县| 天祝| 兴山县| 通化县| 阳东县| 都安| 淮滨县| 永胜县| 南开区| 突泉县| 浦江县| 苏尼特右旗| 黎平县| 城口县| 扎兰屯市|