Mongodb 的基本使用

來源:互聯網
上載者:User

標籤:

一、cmd串連mongodb 服務

    進入mongodb的bin目錄下:[D:\mongodb3.2.5\bin]$ mongo 127.0.0.1:27017

   常用查詢:

   show dbs 查看所有資料庫

  use dbName 進入當前資料庫

  show tables 查看該資料庫下的集合

二、curd操作

   查詢:

    1.查全部:db.mydb2_collection.find();

    2.查詢一條:db.mydb2_collection.findOne();

 插入:db.mydb2_collection.insert({m:1});

 修改:db.mydb_collection.update({x:1},{x:999});

 刪除:db.mydb_collection.remove({c:2});

三、索引

  優點:可以加快與所有相關的查詢速度
  缺點:增加磁碟空間,降低寫入效能

 索引類型:

  id索引:自動建立的
  單鍵索引:
  多鍵索引:db.mydb2_collection.insert({x:[1,2,3,4,5]});
  複合索引:當我們的查詢條件不知一個的時候,就需要建立複合索引
    db.mydb2_collection.ensureIndex({x:1,y:1}); 建立
    db.mydb2_collection.find({x:1,y:2}); 使用
  到期索引:1.在一段時間後會到期的索引 2、在索引到期後相應的資料會被刪除
  使用情境:適合儲存在一些時間後會失效的資料比如使用者登入資訊,儲存的日誌
  建立方式:
    注意:儲存在到期索引的欄位必須是指定時間類型,必須是ISODate或者ISODate數組,不能使用時間戳,否則不能自動刪除。
      如果是數組,則按照最小的時間進行刪除
  到期索引不是複合索引
    刪除時間是不精確的(刪除過程是背景程式沒60秒執行一次的進程操作的,刪除操作本身也需要時間)最小時間差60秒
  全文索引:對字串與字串數組建立全文可搜尋的索引
  插入資料:db.mydb2_collection.insert({arrticle:"aa bb cc gg hh"});
  查詢:db.mydb2_collection.find({$text:{$search:"aa"}});
      db.mydb2_collection.find({$text:{$search:"aa bb ee"}}); //多關鍵字 或者 的關係
      db.mydb2_collection.find({$text:{$search:"aa bb -cc"}}); //不包含cc
      db.mydb2_collection.find({$text:{$search:"\"aa\"\"bb\"\"cc\""}});//多關鍵字 且的關係
全文索引相似性:
    查詢:db.mydb2_collection.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}});
       db.mydb2_collection.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}//排序

索引的屬性:
  name屬性
    指定名字建立索引:db.mydb2_collection.ensureIndex({c:1,y:1},{name:"normal_index"});
    根據名字刪除索引:db.mydb2_collection.dropIndex("normal_index");
  unique索引的唯一性:
    db.mydb2_collection.ensureIndex({m:1,n:1},{unique:true})
    作用:在做添加時如果存在就不插入,否則插入
  稀疏性:sparse
    建立:db.mydb2_collection.ensureIndex({m:1},{sparse:true});
    查詢強制使用索引:db.mydb2_collection.find({m:{$exists:false}}).hint("m_1");
  地理位置索引:
    2d:
      建立2d平面索引: db.mydb_location.ensureIndex({w:"2d"});
      位置表示方式:經緯度[經度,緯度]
      取值範圍:經度:[-180,180] 緯度[-90,90]
      插入:db.mydb_location.insert({w:[2,1]});
    near查詢:
        查查與[1,1]最近的點:db.mydb_location.find({w:{$near:[1,1]}});
        查詢與[1,1]相近 10的點:db.mydb_location.find({w:{$near:[1,1],$maxDistance:10}});
    形狀查詢 $geoWithin:
         矩形 $box: db.mydb_location.find({w:{$geoWithin:{$box:[[0,0],[2,2]]}}});
         圓形$center:db.mydb_location.find({w:{$geoWithin:{$center:[[0,0],2]}}});
          多邊形$polygon:db.mydb_location.find({w:{$geoWithin:{$polygon:[[0,0],[2,2],[0,2]]}}});
     geoNear查詢:
        db.runCommand({geoNear:"mydb_location",near:[1,2],maxDistance:3,num:1});
  2dsphere索引球面地理位置:

四、安全機制

MongoDB安全機制:
1.物理分離(最安全):不現實
2.網路隔離
3.防火牆的隔離:給固定的ip存取權限
4.使用者名稱和密碼

建立使用者
  db.createUser({user:"jalja",pwd:"jalja",roles:[{role:"userAdmin",db:"mydb"},{role:"read",db:"test"}]});

  許可權:read、readWrite、dbAdmin、dbOwner、userAdmin

  在mongo.conf 中添加 auth =true 開啟許可權認證

  使用 使用者名稱密碼串連:mongo 127.0.0.1:27017 -u jalja -p jalja

  mongodb 的角色
  1、資料庫角色
  2、叢集角色
  3、資料備份角色
  4、其他特殊許可權

Mongodb 的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.