MongoDB資料庫學習筆記

來源:互聯網
上載者:User

標籤:mongodb


一、Mongodb資料庫之增刪改查


show databases
show dbs    //顯示資料庫;

show tables   
show collections    //查示表或者集合;

use imooc  //使用或建立資料庫imooc;


增:
use imooc
db.imooc_collection.insert({x:1})    //往集合名“imooc_collection”插入單條資料“x:1”;

db.imooc_collection.insert({x:2})    //往集合名“imooc_collection”插入單條資料“x:2”;
db.imooc_collection.insert({x:100,y:100,z:100})    //往集合名“imooc_collection”插入多欄位單條資料;
for(i=3;i<100;i++)db.imooc_collection.insert({x:i})    //往集合名“imooc_collection”批量插入資料“x:3-99”;


刪:
db.imooc_collection.remove({c:2})    //刪除集合或表匹配的資料(必須傳遞參數,否則報錯);
db.imooc_collection.drop()    //刪除集合或表;

use imooc
db.dropDatabase()    //刪除資料庫imooc;


改:
db.imooc_collection.update({x:1},{x:999})    //更改集合名“imooc_collection”裡“x:1”為“x:999”;
db.imooc_collection.update({z:100},{$set:{y:99}})    //當尋找“z:100”時,只更新y欄位為“y:99”;
db.imooc_collection.update({y:100},{y:999},true)    //當尋找的資料不存在時,插入一條資料;
db.imooc_collection.update({c:1},{$set:{c:2}},false,true)    //批次更新所有匹配的資料;

註:mongodb預設只更新尋找到的第一條資料,目的是防止update誤操作;


查:
db.imooc_collection.find()    //查詢集合名“imooc_collection”裡的所有資料內容;
db.imooc_collection.find().count()    //統計集合名“imooc_collection”裡的所有資料內容的條數;
db.imooc_collection.find().skip(3)    //查詢過濾前3條資料;
db.imooc_collection.find().limit(10)    //查詢限制只顯示前10條資料;
db.imooc_collection.find().sort({x:1})    //查詢使用“x”進行排序;


二、索引


增:
db.collection.ensureIndex()

刪:
db.collection.dropIndex("index name")

改:

查:
db.collection.getIndexes()

1._id索引:

use imooc
db.table2.insert({x:1})
db.table2.getIndexes()    //_id索引是插入資料時,預設自動建立的唯一索引(該命令為查詢集合內的索引);

2.單鍵索引:

db.table2.ensureIndex({x:1})    //建立“x:1”的單鍵索引,查詢條件為一個值時即為單鍵索引;


3.多鍵索引:

db.table2.insert({x:[1,2,3,4,5]})    //插入一個數組,再建立的索引即為多鍵索引;


4.複合索引:

db.table2.ensureIndex({x:1,y:2})    //查詢條件不只一個時建立的索引即為複合索引;


5.到期索引:

db.table2.insert({time:new Date()})    //插入time:當期時間的一條資料;
db.table2.ensureIndex({time:1},{expireAfterSeconds:30})    //建立索引值為time,到期時間為30後的到期索引;

註:資料必須是ISOdate或者ISODate數組,才能自動刪除;每60秒執行檢測一次;


6.全文索引:

db.table2.ensureIndex({key:"text"})
db.table2.ensureIndex({key_1:"text",ke_2:"text"})
db.table2.ensureIndex({"$**":"text"})    //建立全文索引的三種方法;

db.table2.find({$text:{$search:"aa"}})
db.table2.find({$text:{$search:"aa bb cc"}})
db.table2.find({$text:{$search:"aa bb -cc"}})
db.table2.find({$text:{$search:"\"aa\" \"bb\""}})
db.table2.find({text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})    //使用全

文索引的五種方法;

註:全文索引只能指定一個“text”;MongoDB暫不支援中文;

索引重要屬性:
1.名字(name);
2.唯一性(unique);
3.稀疏性(sparse);
4.是否定時刪除(expireAfterSeconds);


7.地理位置索引:

7.1 2d索引

7.2 2dsphere索引


三、mongoDB安全:


1.開啟許可權認證;
2.建立使用者;
3.建立使用者角色;



本文出自 “方寸小山” 部落格,請務必保留此出處http://63638790.blog.51cto.com/513514/1650031

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.