MongoDB基礎操做

更多內容歡迎來到博客 :https://imjianjian.github.io


MongoDB版本:3.4
git

查詢文檔

find()github

查詢全部

db.collection.find({})
db.collection.find()

文檔查詢

//查詢name="jianjian"的全部文檔
db.collection.find({"collection":"jianjian"})

條件操做符

  • $lt 小於
  • $gt 大於
  • $gte 大於等於
  • $lte 小於等於
  • $ne 不等於
  • $all 全包含
  • $in 包含
  • $nin 不包含
//查詢年齡小於18的
db.collection.find("age":{$lt:"18"})

$all

必須知足全部值sql

//能夠查詢出來{age:[20,21,22]}可是查詢不出來{age:[20,22,23]},即必定要有20和21.
db.collection.find({age:{$all:[20,21]}});

$in

這個與$all不同,查詢的值在$in給出的範圍以內就均可以查出來。mongodb

//{age:[20,21,22]},{age:[20,22,23]}都能查出來
db.collection.find({age:{$in:[20,21]}});

$exists

判斷key是否存在數據庫

判斷age字段是否存在
db.collection.find({age:{$exists:true}});

null值的處理

null值處理須要注意的是,不單單能夠查詢出來某一字段值爲null的記錄,還能夠查出來不存在某一字段的記錄。數組

db.collection.find({age:null})

能夠查詢出來age爲null的記錄,以及沒有age字段的記錄。若是咱們須要只去查詢存在age字段而且age字段的值爲null的記錄,須要配合exists操做。數據結構

db.collection.find({age:{"$in":[null],"$exists":true}});

限制查詢條數

limit(number)性能

number爲條數網站

//查詢條數限制爲10條
db.collection.find().limit(10)

排序

sort({key:1})code

key爲須要排序的字段,升序爲1,降序爲-1

//按照年齡升序排序
db.collection.find().sort({"age":1})

跳過記錄

skip()

查詢第10到20條的記錄
db.collection.find().skip(10).limit(10);

查詢文檔條數

count()

age大於25的條數
db.collection.find({age: {$gte: 25}}).count();

插入文檔

insert()基本語法以下:

db.collection.insert(
   <document or array of documents>,
   {
     writeConcern: <document>,
     ordered: <boolean>
   }
)

單條插入

db.collection.insert({"name":"jianjian"})

多條插入

db.collection.insert([{"name":"jianjian"},{"name":"dongdong"}])

錯誤的語法

db.collection.insert({"age":11},{"age":12})

只有age:11被插入進去,因爲接收的插入文檔不是數組

容易誤導的地方:

db.collection.insert([{"name":"jianjian"},{"name":"dongdong"}])

db.collection.insert({"name":"jianjian"},{"age":"dongdong"})

db.collection.insert({"name":"jianjian","name":"dongdong"})

第一個插入是插入兩個文檔,第二個插入是插入一個文檔,第三個雖然也是一個文檔可是因爲鍵重複,因此只有後一個值會被插入age:12

刪除文檔

remove() 方法的基本語法格式以下所示:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }

參數說明:

  • query :(可選)刪除的文檔的條件。
  • justOne : (可選)若是設爲 true 或 1,則只刪除一個文檔。
  • writeConcern :(可選)拋出異常的級別。

刪除num大於100的數據

db.new.remove({"num":{$gt:100}});

刪除new集合全部數據

db.new.remove({});

刪除num等於100的數據

db.new.remove({"num":100})

更新文檔

update() 方法用於更新已存在的文檔。語法格式以下:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

參數說明:

  • query : update的查詢條件,相似sql update查詢內where後面的。
  • update : update的對象和一些更新的操做符(如$,$inc...)等,也能夠理解爲sql update查詢內set後面的

upsert : 可選,這個參數的意思是,若是不存在update的記錄,是否插入objNew,true爲插入,默認是false,不插入。

  • multi : 可選,mongodb 默認是false,只更新找到的第一條記錄,若是這個參數爲true,就把按條件查出來多條記錄所有更新。
  • writeConcern :可選,拋出異常的級別。

$set修改符

用於修改鍵的值,若是鍵不存在就增長鍵

//將age=10的數據改爲15,默認若是age=10的有多條記錄只更新第一條
db.collection.update({"name":"li"},{$set:{"age":10}})

//更新多個知足條件的值,同時若是更新的值不存在就插入更新的值,注意:這裏插入的值是20不是30
db.collection.update({"age":30},{$set:{"age":20}},{multi:true,upsert:true})

能夠省略multi,upsert,這裏不能有花括號,可是不建議這樣作
db.collection.update({"age":30},{$set:{"age":20}},true,true)

//值更新爲數組
db.collection.update({"name":"zhang"},{$set:{"age":[10,
12,14]}},{upsert:true})

//修改成其它的值
db.collection.update({"name":"zhang"},{$set:{"age":''}},{upsert:true})
db.collection.update({"name":"zhang"},{$set:{"age":null}},{upsert:true})

$inc修改符

用於增長已有鍵的值,若是鍵不存在就建立,只能用於整形、長整型、浮點型。

//將name=zhang的記錄的age鍵+10
db.collection.update({"name":"zhang"},{$inc:{"age":10}},{upsert:true})

//將name=zhang的記錄的age鍵-10
db.collection.update({"name":"zhang"},{$inc:{"age":-10}},{upsert:true})

$unset修改符

刪除鍵相似關係數據庫的刪除字段操做,要區別$set修改符的將鍵設空或者null值

db.collection.update({"name":"zhang"},{$unset:{"age":1}})

$push修改符

若是數組已經存在,「$push」會向已有的數組末尾加入一個元素,要是沒有就建立一個新的數組。注意:必須是數組才能加入新的值

db.collection.update({"name":"zhang"},{$push:{"comments":{"email":"abc@qq.com","address":"beijing"}}});

//再插入一條comments
db.collection.update({"name":"zhang"},{$push:{"comments":{"mark":"aaa"}}});

$each修改符

向數組中添加元素

db.collection.insert({"title":1,"list":[1,2,3]})

//向list數組中添加單個元素
db.collection.update({"title":1},{$push:{"list":4}})

//向list數組中添加多個元素
db.collection.update({"title":1},{$push:{"list":{$each:[6,7]}}});

$addToSet修改符

往數組集中插入元素時,若是元素存在就不插入;方法和$push同樣,惟一的區別就是$push不會判斷重複值,重複也能夠插入。$addToSet也能夠結合$each一塊兒使用方法和$push同樣能夠同時往數組中插入多個元素而且若是元素存在則不插入。

db.collection.update({"title":1},{$addToSet:{"list":2}})

db.collection.update({"title":1},{$addToSet:{"list":{$each:[2,3,4]}}});

$pull修改符

匹配數組中的元素將匹配上的元素所有刪除,注意只能對數組中的元素進行匹配

db.collection.insert({"title":1,"list":[1,2,3]});

//清除list數組中的元素2
db.collection.update({},{$pull:{"list":2}})

$pop修改符

從數組的末端或頭刪除一個元素,$pop:{"list":1}:從末尾刪除一個元素;$pop:{"list":-1}:從開頭刪除一個元素

//向數組list中插入兩個元素
db.collection.update({},{$push:{"list":{$each:[2,4]}}});

//從末尾刪除元素
db.collection.update({},{$pop:{"list":1}});

//從開頭刪除元素
db.collection.update({},{$pop:{"list":-1}});

基於位置的數組修改方法
能夠有兩種方式來定位數組中的元素:

1.經過下標;數組的下標都是從0開始。

2.經過$定位操做符,

db.comment.insert({"title":1,"comments":[{"comment":"a","author":"chen","age":10},{"comment":"b","author":"wang","age":30},{"comment":"c","author":"zhang","age":40}]});
//將數組中的第一個列表的age值改爲20
//方法1:
db.comment.update({},{$set:{"comments.0.age":20}});

//方法2:
db.comment.update({"comments.author":"chen"},{$set:{"comments.$.age":20}});

索引

索引一般可以極大的提升查詢的效率,若是沒有索引,MongoDB在讀取數據時必須掃描集合中的每一個文件並選取那些符合查詢條件的記錄。

這種掃描全集合的查詢效率是很是低的,特別在處理大量的數據時,查詢能夠要花費幾十秒甚至幾分鐘,這對網站的性能是很是致命的。

索引是特殊的數據結構,索引存儲在一個易於遍歷讀取的數據集合中,索引是對數據庫表中一列或多列的值進行排序的一種結構

ensureIndex() 方法

MongoDB使用 ensureIndex() 方法來建立索引。

設置No爲索引:

db.collection.ensureIndex({"No":1})
1爲指定按升序建立索引,若是你想按降序來建立索引指定爲-1便可。

刪除集合

刪除test集合,注意drop後面的括號裏面不須要帶花括號

db.test.drop()
相關文章
相關標籤/搜索