新聞中心
Hello,大家好。我是吳老板。

創(chuàng)新互聯(lián)公司專(zhuān)注于崇仁企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都商城網(wǎng)站開(kāi)發(fā)。崇仁網(wǎng)站建設(shè)公司,為崇仁等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
前言
MongoDB 是非關(guān)系型數(shù)據(jù)庫(kù)的代表,一款基于鍵值儲(chǔ)存的高性能數(shù)據(jù)庫(kù)。常作為爬蟲(chóng)儲(chǔ)存數(shù)據(jù)庫(kù)。
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由 C++ 語(yǔ)言編寫(xiě)。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。
參考資料:
https://www.runoob.com/mongodb/mongodb-tutorial.html
連接 MongoDB
格式: mongodb://username:password@host:port/database
- mongodb://:固定的連接頭,必須要指定。
- username:password:用戶(hù)名密碼驗(yàn)證,如果密碼為空可不填
- host:指定host, URI 是唯一必填項(xiàng)。它指定了要連接服務(wù)器的地址。
- port:指定端口,如果不填,默認(rèn)為 27017
- database:若不指定,默認(rèn)打開(kāi) test 數(shù)據(jù)庫(kù)。
創(chuàng)建數(shù)據(jù)庫(kù)、集合
嘗試創(chuàng)建數(shù)據(jù)庫(kù) 地下交通站
我們使用中文命名我們的第一個(gè)數(shù)據(jù)庫(kù)
- > use 地下交通站
- switched to db 地下交通站
- > db
- 地下交通站
如果該數(shù)據(jù)庫(kù)已存在則會(huì)切換到此庫(kù),如果沒(méi)有,則創(chuàng)建。
查看數(shù)據(jù)庫(kù)列表
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
在 MongoDB中,數(shù)據(jù)庫(kù)必須要有數(shù)據(jù)才能在列表中看到它,這一點(diǎn)和其他數(shù)據(jù)庫(kù)還是有很大的不同,我們將在稍后嘗試插入數(shù)據(jù)。
嘗試創(chuàng)建集合 Quotations
- > db.createCollection("Quotations")
- { "ok" : 1 }
這樣一個(gè)集合就創(chuàng)建成功了。
查看 數(shù)據(jù)庫(kù)地下交通站 中的所有集合
- > show collections
- Quotations
刪庫(kù)就更簡(jiǎn)單了,跑路才難 !!
刪除一個(gè)數(shù)據(jù)庫(kù)
- > use 地下交通站
- switched to db 地下交通站
- > db.dropDatabase()
- { "dropped" : "地下交通站", "ok" : 1 }
這樣一個(gè)MongoDB數(shù)據(jù)庫(kù)就沒(méi)了。
刪除一個(gè)集合
- > use 地下交通站
- switched to db 地下交通站
- > db.Quotations.drop() # 刪除集合
這樣數(shù)據(jù)庫(kù)地下交通站 中的Quotations集合就沒(méi)了 !
插入文檔
MongoDB 是一個(gè)面向文檔存儲(chǔ)的數(shù)據(jù)庫(kù),操作起來(lái)比較簡(jiǎn)單和容易。
MongoDB 中一條數(shù)據(jù)被視為一個(gè)文檔,而一個(gè)表則被稱(chēng)為一個(gè)集合(Collection)。
db.collection.insertOne() 用于向集合插入一個(gè)新文檔(單個(gè)json對(duì)象)
db.collection.insertMany() 用于向集合插入一個(gè)多個(gè)文檔(json對(duì)象列表)
嘗試插入一條數(shù)據(jù)
在數(shù)據(jù)庫(kù)地下交通站中的Quotations 集合中插入一條數(shù)據(jù)
- > use 地下交通站
- switched to db 地下交通站
- > db.Quotations.insert({
- name: '賈貴',
- description: '老子在這欠的飯錢(qián)夠你吃二年的',
- createDate: '2021-04-15'
- })
- WriteResult({ "nInserted" : 1 })
查看已插入文檔記錄
- > db.Quotations.find({})
- { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "老子在這欠的飯錢(qián)夠你吃二年的", "createDate" : "2021-04-15" }
可以看到 MongoDB 集合自動(dòng)幫我們生成了 _id 字段用于唯一索引 !!
嘗試插入多條數(shù)據(jù)
- > db.Quotations.insertMany([
- {name: '黃金標(biāo)',description: '這秤砣也TM燉熟了',createDate: '2021-04-15'},
- {name: '賈貴',description: '我捂著腦袋捂著臉撅著屁股就跟他打起來(lái)了',createDate: '2021-04-16'},
- {name: '黑藤',description: '你說(shuō)我他么是誰(shuí),我他么是黑藤',createDate: '2021-04-16'},
- {name: '孫友福',description: '沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢',createDate: '2021-04-17'}
- ])
- > db.Quotations.find({})
- { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "老子在這欠的飯錢(qián)夠你吃二年的", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標(biāo)", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "我捂著腦袋捂著臉撅著屁股就跟他打起來(lái)了", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "你說(shuō)我他么是誰(shuí),我他么是黑藤", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
更新文檔
嘗試更新 name 字段(單條文檔)
- > db.Quotations.update({'name':'黑藤'},{$set:{name: '黑藤',description: '天下漢奸一般蠢',createDate: '2021-04-16'}})
更改生效
- > db.Quotations.find({}) 1-04-16'}})
- { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "老子在這欠的飯錢(qián)夠你吃二年的", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標(biāo)", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "我捂著腦袋捂著臉撅著屁股就跟他打起來(lái)了", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
嘗試更新所有文檔
- > db.Quotations.update({'name':'賈貴'},{$set: {name: '賈貴',description: '這我哪知道呀!我知道她長(zhǎng)的嘿!',createDate: '2021-04-16'}},{multi:true})
- > db.Quotations.find({})
- { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長(zhǎng)的嘿!", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標(biāo)", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長(zhǎng)的嘿!", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
這里我們將 multi 參數(shù)設(shè)置成 True,意味著將更新所有匹配查詢(xún)條件的文檔
發(fā)現(xiàn)所有name 為賈貴的語(yǔ)錄都改變了。
不存在則新增
設(shè)置 upsert 參數(shù)為T(mén)rue, 更新不存在則新增。
- > db.Quotations.update({'name':'瀨川'},{$set:{ 'name' : '瀨川', 'description' : '去東關(guān),搗亂的干活'}},{upsert:true})
- > db.Quotations.find({})
- { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長(zhǎng)的嘿!", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標(biāo)", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長(zhǎng)的嘿!", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
- { "_id" : ObjectId("6078ee01249a48b5bb83b936"), "name" : "瀨川", "description" : "去東關(guān),搗亂的干活" }
發(fā)現(xiàn)新增了一條 name 為 瀨川 的文檔記錄
與此同時(shí), 我建議 使用 update_one 方法更新單個(gè)文檔, 使用 update_many 方法更新多個(gè)文檔。
刪除文檔
刪除匹配查詢(xún)條件的所有文檔
- > db.Quotations.remove({'name':'賈貴'})
- WriteResult({ "nRemoved" : 2 })
- > db.Quotations.find({})
- { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標(biāo)", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
- { "_id" : ObjectId("6078ee01249a48b5bb83b936"), "name" : "瀨川", "description" : "去東關(guān),搗亂的干活" }
這樣就把 name 為 賈貴的所有文檔記錄全都干掉了。
刪除集合中所有文檔
- > db.Quotations.remove({})
{} 表示無(wú)條件,默認(rèn)移除全部文檔,等價(jià)于刪除此集合。
查詢(xún)文檔
MongoDB 查詢(xún)數(shù)據(jù)的通用語(yǔ)法
- db.collection.find(query, projection)
條件查詢(xún)
指定非 _id 字段 查詢(xún)
- > use 地下交通站
- switched to db 地下交通站
- > db.Quotations.find({},{'_id':0})
- { "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
- { "name" : "瀨川", "description" : "去東關(guān),搗亂的干活" }
- { "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
- { "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
為了美化輸出,可以使用 pretty() 方法,此方法對(duì) sql 事務(wù)不起到任何意義
- db.Quotations.find({},{'_id':0}).pretty()
AND 查詢(xún)
查詢(xún) [ name 為 黃金標(biāo) ] 并且 [ createDate 為 2021-04-26 ] 的所有文檔記錄
- > db.Quotations.find({"name":"黃金標(biāo)", "createDate":"2021-04-26"})
- { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
OR 查詢(xún)
查詢(xún) [ name 為 賈貴 ] 或者 [ description 為 天下漢奸一般蠢 ] 的所有文檔記錄
- > db.Quotations.find({$or:[{"name":"賈貴"},{"description": "天下漢奸一般蠢"}]})
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
可以看到在這里 OR 查詢(xún)使用了顯式操作($or 后接條件列表), OR操作一定是顯式的,不存在隱式的OR操作。
而上面的 AND查詢(xún) 操作是不是可以嘗試也寫(xiě)成 顯式操作
- > db.Quotations.find({$and:[{"name":"黃金標(biāo)"},{"createDate": "2021-04-26"}]})
- { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
發(fā)現(xiàn)也是可以的 !!
AND 和 OR 聯(lián)合使用
查詢(xún) [ createDate 為 2021-04-18 ] 并且 [ [ name 為 賈貴 ] 或者 [ description 為 天下漢奸一般蠢 ] ] 的所有文檔記錄
偽sql語(yǔ)句表述為:
- where createDate='2021-04-18' AND (name = '賈貴' OR description = '天下漢奸一般蠢')
- > db.Quotations.find({"createDate": "2021-04-18", $or: [{"name": "賈貴"},{"description": "天下漢奸一般蠢"}]})
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
高級(jí)查詢(xún)
范圍查詢(xún)、正則查詢(xún)
- $lt 小于
- $gt 大于
- $lte 小于等于
- $gte 大于等于
- $ne 不等于
- $in 在范圍內(nèi)
- $nin 不在范圍內(nèi)
查詢(xún)創(chuàng)建時(shí)間 大于 2021-04-17 的所有文檔記錄
- > db.Quotations.find({"createDate" : {$gt : "2021-04-17"}})
- { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
查詢(xún) [ name 不等于 黃金標(biāo) ] 并且 [ createDate 小于 2021-04-26 ] 的所有文檔記錄
- > db.Quotations.find({"name" : {$ne : "黃金標(biāo)"},"createDate":{$lt : "2021-04-26"}})
- { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
查詢(xún) [ name 包含 黃金標(biāo)、賈貴 ] 并且 [ createDate 大于 2021-04-02 ] 的所有文檔記錄
- > db.Quotations.find({"name" : {$in : ["黃金標(biāo)","賈貴"]},"createDate":{$gt : "2021-04-02"}})
- { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- $regex 匹配正則
- $exists 屬性是否存在
- $type 類(lèi)型判斷
- $mod 數(shù)字模操作
- $text 文本查詢(xún)
- $where 高級(jí)條件查詢(xún)
利用正則語(yǔ)句匹配 name 中帶有 金標(biāo) 的所有文檔
- > db.Quotations.find({"name":{"$regex":"金標(biāo)"}})
- { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
利用正則語(yǔ)句匹配 以 我 開(kāi)頭的 description 的所有文檔
- > db.Quotations.find({"description":{"$regex":"^我"}})
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
利用正則語(yǔ)句匹配 以 我 開(kāi)頭、以 屎 結(jié)尾 description 的所有文檔
- > db.Quotations.find({"description":{"$regex":"^我.*屎$"}})
- { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
更多高級(jí)查詢(xún)用法各位讀者請(qǐng)參考 MongoDB 官方文檔
聚合函數(shù)
排序
在 MongoDB 中使用 sort() 方法對(duì)數(shù)據(jù)進(jìn)行排序,sort() 方法可以通過(guò)參數(shù)指定排序的字段, 并使用 1 和 -1 來(lái)指定排序的方式, 其中 1 為升序排列,而 -1 是用于降序排列。
我們總是喜歡用時(shí)間維度來(lái)排序集合中的文檔
- > db.Quotations.find({},{"_id":0}).sort({"createDate":-1})
- { "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
- { "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
- { "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17" }
- { "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
- { "name" : "瀨川", "description" : "去東關(guān),搗亂的干活" }
分組
- db.Quotations.aggregate({$group:{_id:{name:"$name"}}})
根據(jù) name 和 description 字段進(jìn)行分組
- db.Quotations.aggregate({
- $group:{
- _id:{name:"$name",description:"$description"}
- }})
由于我們現(xiàn)有集合文檔的字段中并沒(méi)有可計(jì)算的數(shù)值
我們嘗試添加一個(gè)集合字段并先賦值為空
- db.Quotations.update({},{$set:{"forceValue": ""}},{multi:true})
添加隨機(jī)值
- db.Quotations.update({"name":"黑藤"},{$set:{"forceValue": Math.random()*100}},{multi:true})
- db.Quotations.update({"name":"孫友福"},{$set:{"forceValue": Math.random()*100}},{multi:true})
- db.Quotations.update({"name":"瀨川"},{$set:{"forceValue": Math.random()*100}},{multi:true})
- db.Quotations.update({"name":"黃金標(biāo)"},{$set:{"forceValue": Math.random()*100}},{multi:true})
- db.Quotations.update({"name":"賈貴"},{$set:{"forceValue": Math.random()*100}},{multi:true})
結(jié)果如下
- { "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16", "forceValue" : 14.796604243632927 }
- { "name" : "孫友福", "description" : "沒(méi)有水就沒(méi)有魚(yú),沒(méi)有你就沒(méi)有驢", "createDate" : "2021-04-17", "forceValue" : 43.71427504449847 }
- { "name" : "瀨川", "description" : "去東關(guān),搗亂的干活", "forceValue" : 41.53502198761502 }
- { "name" : "黃金標(biāo)", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。", "forceValue" : 21.887495918176125 }
- { "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎", "forceValue" : 94.18697675337746 }
由于要用到分組,我們需要多添加幾條文檔數(shù)據(jù)
- db.Quotations.insertMany([
- {name: '黃金標(biāo)',description: '我黃某人為官一任就得保一方平安',forceValue:Math.random()*100,createDate: '2021-04-11'},
- {name: '賈貴',description: '皇軍沒(méi)來(lái)的時(shí)候,你欺負(fù)我,皇軍來(lái)了你還欺負(fù)我,那皇軍不是TM白來(lái)了嗎',forceValue:Math.random()*100,createDate: '2021-04-14'},
- {name: '黑藤',description: '全東亞乃至全亞洲都找不到第二張想你這么一張空前絕后的臉來(lái)',forceValue:Math.random()*100,createDate: '2021-04-22'},
- {name: '賈貴',description: '這我哪知道啊,我就知道她長(zhǎng)得嘿',forceValue:Math.random()*100, createDate: '2021-04-19'}
- ])
分組使用聚合函數(shù)
常用聚合函數(shù)
- $sum
- $avg
- $max
- $min
- $first
- $last
根據(jù) name 分組并使用 $sum 函數(shù)求和,得到一下結(jié)果
- $match 查詢(xún)條件
- $group 根據(jù)字段分組(可以是多字段) + 聚合函數(shù)
- > db.Quotations.aggregate(
- ... {"$match":{"createDate":{"$gt":"2021-04-07"}}},
- ... {"$group":{"_id":"$name",'forceValue':{"$sum":"$forceValue"}}},
- ... )
- { "_id" : "賈貴", "forceValue" : 204.22774268609504 }
- { "_id" : "黃金標(biāo)", "forceValue" : 121.14812944012357 }
- { "_id" : "黑藤", "forceValue" : 91.2583132098972 }
- { "_id" : "孫友福", "forceValue" : 43.71427504449847 }
這相當(dāng)于在 Mysql 中執(zhí)行
- select id,sum(forceValue) from Quotations where createDate > "2021-04-07" group by name;
$avg 求平均
- > db.Quotations.aggregate(
- ... {"$match":{"createDate":{"$gt":"2021-04-07"}}},
- ... {"$group":{"_id":"$name",'forceValue':{"$avg":"$forceValue"}}},
- ... )
- { "_id" : "賈貴", "forceValue" : 68.07591422869835 }
- { "_id" : "黃金標(biāo)", "forceValue" : 60.574064720061784 }
- { "_id" : "黑藤", "forceValue" : 45.6291566049486 }
- { "_id" : "孫友福", "forceValue" : 43.71427504449847 }
多字段分組
根據(jù) createDate + name 進(jìn)行分組之后求和
- > db.Quotations.aggregate(
- ... ... {"$match":{"createDate":{"$gt":"2021-04-07"}}},
- ... ... {"$group":{"_id":{"createDate":"$createDate","name":"$name"},'forceValue':{"$sum":"$forceValue"}}},
- ... ... )
- { "_id" : { "createDate" : "2021-04-11", "name" : "黃金標(biāo)" }, "forceValue" : 99.26063352194744 }
- { "_id" : { "createDate" : "2021-04-16", "name" : "黑藤" }, "forceValue" : 14.796604243632927 }
- { "_id" : { "createDate" : "2021-04-17", "name" : "孫友福" }, "forceValue" : 43.71427504449847 }
- { "_id" : { "createDate" : "2021-04-18", "name" : "賈貴" }, "forceValue" : 94.18697675337746 }
- { "_id" : { "createDate" : "2021-04-26", "name" : "黃金標(biāo)" }, "forceValue" : 21.887495918176125 }
- { "_id" : { "createDate" : "2021-04-14", "name" : "賈貴" }, "forceValue" : 81.9931941785778 }
- { "_id" : { "createDate" : "2021-04-22", "name" : "黑藤" }, "forceValue" : 76.46170896626427 }
- { "_id" : { "createDate" : "2021-04-19", "name" : "賈貴" }, "forceValue" : 28.04757175413979 }
我不能再繼續(xù)寫(xiě)下去了,MongoDB 的基礎(chǔ)入門(mén)就介紹到這里
更多教程請(qǐng)參考 MongoDB官方文檔
結(jié)束語(yǔ)
我是吳老板,以上就是我們這次的干貨分享了。最后重申下:
MongoDB 是非關(guān)系型數(shù)據(jù)庫(kù)的代表,一款基于鍵值儲(chǔ)存的高性能數(shù)據(jù)庫(kù)。常作為爬蟲(chóng)儲(chǔ)存數(shù)據(jù)庫(kù)。
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由 C++ 語(yǔ)言編寫(xiě)。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。
網(wǎng)站標(biāo)題:一文帶你搞懂爬蟲(chóng)儲(chǔ)存數(shù)據(jù)庫(kù)MongoDB
網(wǎng)頁(yè)路徑:http://m.fisionsoft.com.cn/article/dhccccc.html


咨詢(xún)
建站咨詢(xún)
