爲何使用索引?sql
假設有一本書,你想看第六章第六節講的是什麼,你會怎麼作,通常人確定去看目錄,找到這一節對應的頁數,而後翻到這一頁。這就是目錄索引,幫助讀者快速找到想要的章節。在數據庫中,咱們也有索引,其目的固然和咱們翻書同樣,能幫助咱們提升查詢的效率。索引就像目錄同樣,減小了計算機工做量,對於表記錄較多的數據庫來講是很是實用的,能夠大大的提升查詢的速度。不然的話,若是沒有索引,計算機會一條一條的掃描,每一次都要掃描全部的記錄,浪費大量的cpu時間。mongodb
爲了查詢方便,咱們建立一個擁有500000條數據的一個集合數據庫
> for(var i=0;i<500000;i++){db.nums.insert({name:"name"+i,age:i})} WriteResult({ "nInserted" : 1 })
注意在 3.0.0 版本前建立索引方法爲 db.collection.ensureIndex(),以後的版本使用了 db.collection.createIndex() 方法,ensureIndex() 還能用,但只是 createIndex() 的別名。數組
>db.collection.createIndex(keys, options)
語法中 Key 值爲你要建立的索引字段,1 爲指定按升序建立索引,若是你想按降序來建立索引指定爲 -1 便可。spa
實例:code
一、先在未建立索引以前咱們按需求查找nums集合裏面age爲399999的 blog
二、在建立索引以後查詢age爲399999的排序
建立索引索引
> db.nums.createIndex({age:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
經過兩次執行時間的對比明顯能夠看到建立索引後查詢更快,數據越多,體現的越明顯。ip
createIndex() 接收可選參數,可選參數列表以下:
MongoDB中聚合(aggregate)主要用於處理數據(諸如統計平均值,求和等),並返回計算後的數據結果。有點相似sql語句中的 count(*)。
語法:aggregate() 方法的基本語法格式以下所示:
db.集合名稱.aggregate([{管道:{表達式}}])
ps ajx | grep mongo
表達式:處理輸入文檔並輸出
表達式:'$列名'
經常使用表達式
例如:heros表中數據以下
> db.heros.find().pretty() { "_id" : ObjectId("5d2e0647614bec7ca4687792"), "h_name" : "後裔", "h_skill" : "懲戒之劍", "h_attack" : 1000, "h_blood" : 800, "h_type" : "射手" } { "_id" : ObjectId("5d2e0685614bec7ca4687793"), "h_name" : "李白", "h_skill" : "青蓮劍仙", "h_attack" : 1400, "h_blood" : 900, "h_type" : "刺客" } { "_id" : ObjectId("5d2e06d6614bec7ca4687794"), "h_name" : "韓信", "h_skill" : "國士無雙", "h_attack" : 1300, "h_blood" : 850, "h_type" : "刺客" } { "_id" : ObjectId("5d2e0720614bec7ca4687795"), "h_name" : "妲己", "h_skill" : "女王崇拜", "h_attack" : 1200, "h_blood" : 750, "h_type" : "法師" }
例如:按照英雄類型分組,進行統計個數
> db.heros.aggregate([{$group:{_id:"$h_type",counter:{$sum:1}}}]) { "_id" : "刺客", "counter" : 2 } { "_id" : "法師", "counter" : 1 } { "_id" : "射手", "counter" : 1 } >
例如:求英雄的從攻擊力和平均血量
> db.heros.aggregate([{$group:{_id:null,h_attacks:{$sum:"$h_attack"},avgh_blood:{$avg:"$h_blood"}}}]) { "_id" : null, "h_attacks" : 4900, "avgh_blood" : 825 } >
只查詢英雄類型和名字
> db.heros.aggregate([{$group:{_id:"$h_type",name:{$push:"$h_name"}}}]) { "_id" : "刺客", "name" : [ "李白", "韓信" ] } { "_id" : "法師", "name" : [ "妲己" ] } { "_id" : "射手", "name" : [ "後裔" ] } >
> db.heros.aggregate([{$group:{_id:"h_type",name:{$push:"$$ROOT"}}}]).pretty() { "_id" : "h_type", "name" : [ { "_id" : ObjectId("5d2e0647614bec7ca4687792"), "h_name" : "後裔", "h_skill" : "懲戒之劍", "h_attack" : 1000, "h_blood" : 800, "h_type" : "射手" }, { "_id" : ObjectId("5d2e0685614bec7ca4687793"), "h_name" : "李白", "h_skill" : "青蓮劍仙", "h_attack" : 1400, "h_blood" : 900, "h_type" : "刺客" }, { "_id" : ObjectId("5d2e06d6614bec7ca4687794"), "h_name" : "韓信", "h_skill" : "國士無雙", "h_attack" : 1300, "h_blood" : 850, "h_type" : "刺客" }, { "_id" : ObjectId("5d2e0720614bec7ca4687795"), "h_name" : "妲己", "h_skill" : "女王崇拜", "h_attack" : 1200, "h_blood" : 750, "h_type" : "法師" } ] } >
例如:查詢攻擊力大於1200
> db.heros.aggregate([{$match:{"h_attack":{$gt:1200}}}]) { "_id" : ObjectId("5d2e0685614bec7ca4687793"), "h_name" : "李白", "h_skill" : "青蓮劍仙", "h_attack" : 1400, "h_blood" : 900, "h_type" : "刺客" } { "_id" : ObjectId("5d2e06d6614bec7ca4687794"), "h_name" : "韓信", "h_skill" : "國士無雙", "h_attack" : 1300, "h_blood" : 850, "h_type" : "刺客" } >
> db.heros.aggregate([{$project:{_id:0,h_name:1,h_skill:1}}]) { "h_name" : "後裔", "h_skill" : "懲戒之劍" } { "h_name" : "李白", "h_skill" : "青蓮劍仙" } { "h_name" : "韓信", "h_skill" : "國士無雙" } { "h_name" : "妲己", "h_skill" : "女王崇拜" } >
對某字段值進行拆分
db.集合名稱.aggregate([{$unwind:'$字段名稱'}])
例如:
db.t2.insert({_id:1,item:'t-shirt',size:['S','M','L']})
查詢:
> db.t2.aggregate([{$unwind:'$size'}]) { "_id" : 1, "item" : "t-shirt", "size" : "S" } { "_id" : 1, "item" : "t-shirt", "size" : "M" } { "_id" : 1, "item" : "t-shirt", "size" : "L" } >
db.inventory.aggregate([{ $unwind:{ path:'$字段名稱', preserveNullAndEmptyArrays:<boolean>#防止數據丟失 } }])
db.t3.insert([ { "_id" : 1, "item" : "a", "size": [ "S", "M", "L"] }, { "_id" : 2, "item" : "b", "size" : [ ] }, { "_id" : 3, "item" : "c", "size": "M" }, { "_id" : 4, "item" : "d" }, { "_id" : 5, "item" : "e", "size" : null } ])
> db.t3.find().pretty() { "_id" : 1, "item" : "a", "size" : [ "S", "M", "L" ] } { "_id" : 2, "item" : "b", "size" : [ ] } { "_id" : 3, "item" : "c", "size" : "M" } { "_id" : 4, "item" : "d" } { "_id" : 5, "item" : "e", "size" : null } > db.t3.aggregate([{$unwind:'$size'}]) { "_id" : 1, "item" : "a", "size" : "S" } { "_id" : 1, "item" : "a", "size" : "M" } { "_id" : 1, "item" : "a", "size" : "L" } { "_id" : 3, "item" : "c", "size" : "M" } >
使用語法2查詢不會丟棄空數組,無字段,null的文檔
> db.t3.aggregate([{$unwind:{path:'$sizes',preserveNullAndEmptyArrays:true}}]) { "_id" : 1, "item" : "a", "size" : [ "S", "M", "L" ] } { "_id" : 2, "item" : "b", "size" : [ ] } { "_id" : 3, "item" : "c", "size" : "M" } { "_id" : 4, "item" : "d" } { "_id" : 5, "item" : "e", "size" : null } >