日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
一文帶你搞懂爬蟲儲存數據庫MongoDB

Hello,大家好。我是吳老板。

創(chuàng)新互聯公司專注于崇仁企業(yè)網站建設,成都響應式網站建設公司,成都商城網站開發(fā)。崇仁網站建設公司,為崇仁等地區(qū)提供建站服務。全流程按需定制設計,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯公司專業(yè)和態(tài)度為您提供的服務

前言

MongoDB 是非關系型數據庫的代表,一款基于鍵值儲存的高性能數據庫。常作為爬蟲儲存數據庫。

MongoDB 是一個基于分布式文件存儲的數據庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數據存儲解決方案。

MongoDB 是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。

參考資料:

https://www.runoob.com/mongodb/mongodb-tutorial.html

連接 MongoDB

格式: mongodb://username:password@host:port/database

  • mongodb://:固定的連接頭,必須要指定。
  • username:password:用戶名密碼驗證,如果密碼為空可不填
  • host:指定host, URI 是唯一必填項。它指定了要連接服務器的地址。
  • port:指定端口,如果不填,默認為 27017
  • database:若不指定,默認打開 test 數據庫。

創(chuàng)建數據庫、集合

嘗試創(chuàng)建數據庫 地下交通站

我們使用中文命名我們的第一個數據庫

 
 
 
  1. > use 地下交通站
  2. switched to db 地下交通站
  3. > db
  4. 地下交通站

如果該數據庫已存在則會切換到此庫,如果沒有,則創(chuàng)建。

查看數據庫列表

 
 
 
  1. > show dbs
  2. admin   0.000GB
  3. config  0.000GB
  4. local   0.000GB

在 MongoDB中,數據庫必須要有數據才能在列表中看到它,這一點和其他數據庫還是有很大的不同,我們將在稍后嘗試插入數據。

嘗試創(chuàng)建集合 Quotations

 
 
 
  1. > db.createCollection("Quotations")
  2. { "ok" : 1 }

這樣一個集合就創(chuàng)建成功了。

查看 數據庫地下交通站 中的所有集合

 
 
 
  1. > show collections
  2. Quotations

刪庫就更簡單了,跑路才難 !!

刪除一個數據庫

 
 
 
  1. > use 地下交通站
  2. switched to db 地下交通站
  3. > db.dropDatabase()
  4. { "dropped" : "地下交通站", "ok" : 1 }

這樣一個MongoDB數據庫就沒了。

刪除一個集合

 
 
 
  1. > use 地下交通站
  2. switched to db 地下交通站
  3. > db.Quotations.drop()  # 刪除集合

這樣數據庫地下交通站 中的Quotations集合就沒了 !

插入文檔

MongoDB 是一個面向文檔存儲的數據庫,操作起來比較簡單和容易。

MongoDB 中一條數據被視為一個文檔,而一個表則被稱為一個集合(Collection)。

db.collection.insertOne() 用于向集合插入一個新文檔(單個json對象)

db.collection.insertMany() 用于向集合插入一個多個文檔(json對象列表)

嘗試插入一條數據

在數據庫地下交通站中的Quotations 集合中插入一條數據

 
 
 
  1. > use 地下交通站
  2. switched to db 地下交通站
  3. > db.Quotations.insert({
  4.    name: '賈貴',
  5.    description: '老子在這欠的飯錢夠你吃二年的',
  6.    createDate: '2021-04-15'
  7.  })
  8. WriteResult({ "nInserted" : 1 })

查看已插入文檔記錄

 
 
 
  1. > db.Quotations.find({})
  2. { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "老子在這欠的飯錢夠你吃二年的", "createDate" : "2021-04-15" }

可以看到 MongoDB 集合自動幫我們生成了 _id 字段用于唯一索引 !!

嘗試插入多條數據

 
 
 
  1. > db.Quotations.insertMany([
  2.          {name: '黃金標',description: '這秤砣也TM燉熟了',createDate: '2021-04-15'},
  3.          {name: '賈貴',description: '我捂著腦袋捂著臉撅著屁股就跟他打起來了',createDate: '2021-04-16'},
  4.          {name: '黑藤',description: '你說我他么是誰,我他么是黑藤',createDate: '2021-04-16'},
  5.          {name: '孫友福',description: '沒有水就沒有魚,沒有你就沒有驢',createDate: '2021-04-17'}
  6.      ])
 
 
 
  1. > db.Quotations.find({})
  2. { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "老子在這欠的飯錢夠你吃二年的", "createDate" : "2021-04-15" }
  3. { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
  4. { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "我捂著腦袋捂著臉撅著屁股就跟他打起來了", "createDate" : "2021-04-16" }
  5. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "你說我他么是誰,我他么是黑藤", "createDate" : "2021-04-16" }
  6. { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }

更新文檔

嘗試更新 name 字段(單條文檔)

 
 
 
  1. > db.Quotations.update({'name':'黑藤'},{$set:{name: '黑藤',description: '天下漢奸一般蠢',createDate: '2021-04-16'}})

更改生效

 
 
 
  1. > db.Quotations.find({})                                                                                 1-04-16'}})
  2. { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "老子在這欠的飯錢夠你吃二年的", "createDate" : "2021-04-15" }
  3. { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
  4. { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "我捂著腦袋捂著臉撅著屁股就跟他打起來了", "createDate" : "2021-04-16" }
  5. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  6. { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }

嘗試更新所有文檔

 
 
 
  1. > db.Quotations.update({'name':'賈貴'},{$set: {name: '賈貴',description: '這我哪知道呀!我知道她長的嘿!',createDate: '2021-04-16'}},{multi:true})
 
 
 
  1. > db.Quotations.find({})                                                                                     
  2. { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長的嘿!", "createDate" : "2021-04-16" }
  3. { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
  4. { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長的嘿!", "createDate" : "2021-04-16" }
  5. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  6. { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }

這里我們將 multi 參數設置成 True,意味著將更新所有匹配查詢條件的文檔

發(fā)現所有name 為賈貴的語錄都改變了。

不存在則新增

設置 upsert 參數為True, 更新不存在則新增。

 
 
 
  1. > db.Quotations.update({'name':'瀨川'},{$set:{ 'name' : '瀨川', 'description' : '去東關,搗亂的干活'}},{upsert:true})
 
 
 
  1. > db.Quotations.find({})                                             
  2. { "_id" : ObjectId("6078e9743252cc07e092d204"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長的嘿!", "createDate" : "2021-04-16" }
  3. { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
  4. { "_id" : ObjectId("6078ebd395708b77ad7a8bd9"), "name" : "賈貴", "description" : "這我哪知道呀!我知道她長的嘿!", "createDate" : "2021-04-16" }
  5. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  6. { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }
  7. { "_id" : ObjectId("6078ee01249a48b5bb83b936"), "name" : "瀨川", "description" : "去東關,搗亂的干活" }

發(fā)現新增了一條 name 為 瀨川 的文檔記錄

與此同時, 我建議 使用 update_one 方法更新單個文檔, 使用 update_many 方法更新多個文檔。

刪除文檔

刪除匹配查詢條件的所有文檔

 
 
 
  1. > db.Quotations.remove({'name':'賈貴'})
  2. WriteResult({ "nRemoved" : 2 })
 
 
 
  1. > db.Quotations.find({})    
  2. { "_id" : ObjectId("6078ebd395708b77ad7a8bd8"), "name" : "黃金標", "description" : "這秤砣也TM燉熟了", "createDate" : "2021-04-15" }
  3. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  4. { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }
  5. { "_id" : ObjectId("6078ee01249a48b5bb83b936"), "name" : "瀨川", "description" : "去東關,搗亂的干活" }

這樣就把 name 為 賈貴的所有文檔記錄全都干掉了。

刪除集合中所有文檔

 
 
 
  1. > db.Quotations.remove({})

{} 表示無條件,默認移除全部文檔,等價于刪除此集合。

查詢文檔

MongoDB 查詢數據的通用語法

 
 
 
  1. db.collection.find(query, projection)

條件查詢

指定非 _id 字段 查詢

 
 
 
  1. > use 地下交通站
  2. switched to db 地下交通站
  3. > db.Quotations.find({},{'_id':0})
  4. { "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  5. { "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }
  6. { "name" : "瀨川", "description" : "去東關,搗亂的干活" }
  7. { "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  8. { "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
  9. { "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }

為了美化輸出,可以使用 pretty() 方法,此方法對 sql 事務不起到任何意義

 
 
 
  1. db.Quotations.find({},{'_id':0}).pretty()

AND 查詢

查詢 [ name 為 黃金標 ] 并且 [ createDate 為 2021-04-26 ] 的所有文檔記錄

 
 
 
  1. > db.Quotations.find({"name":"黃金標", "createDate":"2021-04-26"})
  2. { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  3. { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }

OR 查詢

查詢 [ name 為 賈貴 ] 或者 [ description 為 天下漢奸一般蠢 ] 的所有文檔記錄

 
 
 
  1. > db.Quotations.find({$or:[{"name":"賈貴"},{"description": "天下漢奸一般蠢"}]})
  2. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  3. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }

可以看到在這里 OR 查詢使用了顯式操作($or 后接條件列表), OR操作一定是顯式的,不存在隱式的OR操作。

而上面的 AND查詢 操作是不是可以嘗試也寫成 顯式操作

 
 
 
  1. > db.Quotations.find({$and:[{"name":"黃金標"},{"createDate": "2021-04-26"}]})
  2. { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  3. { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }

發(fā)現也是可以的 !!

AND 和 OR 聯合使用

查詢 [ createDate 為 2021-04-18 ] 并且 [ [ name 為 賈貴 ] 或者 [ description 為 天下漢奸一般蠢 ] ] 的所有文檔記錄

偽sql語句表述為:

 
 
 
  1. where createDate='2021-04-18' AND (name = '賈貴' OR description = '天下漢奸一般蠢')
 
 
 
  1. > db.Quotations.find({"createDate": "2021-04-18", $or: [{"name": "賈貴"},{"description": "天下漢奸一般蠢"}]})
  2. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }

高級查詢

范圍查詢、正則查詢

  • $lt 小于
  • $gt 大于
  • $lte 小于等于
  • $gte 大于等于
  • $ne 不等于
  • $in 在范圍內
  • $nin 不在范圍內

查詢創(chuàng)建時間 大于 2021-04-17 的所有文檔記錄

 
 
 
  1. > db.Quotations.find({"createDate" : {$gt : "2021-04-17"}})
  2. { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  3. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
  4. { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }

查詢 [ name 不等于 黃金標 ] 并且 [ createDate 小于 2021-04-26 ] 的所有文檔記錄

 
 
 
  1. > db.Quotations.find({"name" : {$ne : "黃金標"},"createDate":{$lt : "2021-04-26"}})
  2. { "_id" : ObjectId("6078ebd395708b77ad7a8bda"), "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  3. { "_id" : ObjectId("6078ebd395708b77ad7a8bdb"), "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }
  4. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }

查詢 [ name 包含 黃金標、賈貴 ] 并且 [ createDate 大于 2021-04-02 ] 的所有文檔記錄

 
 
 
  1. > db.Quotations.find({"name" : {$in : ["黃金標","賈貴"]},"createDate":{$gt : "2021-04-02"}})
  2. { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  3. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
  4. { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  • $regex 匹配正則
  • $exists 屬性是否存在
  • $type 類型判斷
  • $mod 數字模操作
  • $text 文本查詢
  • $where 高級條件查詢

利用正則語句匹配 name 中帶有 金標 的所有文檔

 
 
 
  1. > db.Quotations.find({"name":{"$regex":"金標"}})
  2. { "_id" : ObjectId("6078f0e31e12a856cf9525da"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  3. { "_id" : ObjectId("6078f2091e12a856cf9525dd"), "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }

利用正則語句匹配 以 我 開頭的 description 的所有文檔

 
 
 
  1. > db.Quotations.find({"description":{"$regex":"^我"}})
  2. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }

利用正則語句匹配 以 我 開頭、以 屎 結尾 description 的所有文檔

 
 
 
  1. > db.Quotations.find({"description":{"$regex":"^我.*屎$"}})
  2. { "_id" : ObjectId("6078f2091e12a856cf9525dc"), "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }

更多高級查詢用法各位讀者請參考 MongoDB 官方文檔

聚合函數

排序

在 MongoDB 中使用 sort() 方法對數據進行排序,sort() 方法可以通過參數指定排序的字段, 并使用 1 和 -1 來指定排序的方式, 其中 1 為升序排列,而 -1 是用于降序排列。

我們總是喜歡用時間維度來排序集合中的文檔

 
 
 
  1. > db.Quotations.find({},{"_id":0}).sort({"createDate":-1})
  2. { "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  3. { "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。" }
  4. { "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎" }
  5. { "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17" }
  6. { "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16" }
  7. { "name" : "瀨川", "description" : "去東關,搗亂的干活" }

分組

 
 
 
  1. db.Quotations.aggregate({$group:{_id:{name:"$name"}}})

根據 name 和 description 字段進行分組

 
 
 
  1. db.Quotations.aggregate({
  2.     $group:{
  3.         _id:{name:"$name",description:"$description"}
  4. }})

由于我們現有集合文檔的字段中并沒有可計算的數值

我們嘗試添加一個集合字段并先賦值為空

 
 
 
  1. db.Quotations.update({},{$set:{"forceValue": ""}},{multi:true})

添加隨機值

 
 
 
  1. db.Quotations.update({"name":"黑藤"},{$set:{"forceValue": Math.random()*100}},{multi:true})
  2. db.Quotations.update({"name":"孫友福"},{$set:{"forceValue": Math.random()*100}},{multi:true})
  3. db.Quotations.update({"name":"瀨川"},{$set:{"forceValue": Math.random()*100}},{multi:true})
  4. db.Quotations.update({"name":"黃金標"},{$set:{"forceValue": Math.random()*100}},{multi:true})
  5. db.Quotations.update({"name":"賈貴"},{$set:{"forceValue": Math.random()*100}},{multi:true})

結果如下

 
 
 
  1. { "name" : "黑藤", "description" : "天下漢奸一般蠢", "createDate" : "2021-04-16", "forceValue" : 14.796604243632927 }
  2. { "name" : "孫友福", "description" : "沒有水就沒有魚,沒有你就沒有驢", "createDate" : "2021-04-17", "forceValue" : 43.71427504449847 }
  3. { "name" : "瀨川", "description" : "去東關,搗亂的干活", "forceValue" : 41.53502198761502 }
  4. { "name" : "黃金標", "createDate" : "2021-04-26", "description" : "劉副官你記住了,讓皇軍先尿。", "forceValue" : 21.887495918176125 }
  5. { "name" : "賈貴", "createDate" : "2021-04-18", "description" : "我啐他一臉狗屎", "forceValue" : 94.18697675337746 }

由于要用到分組,我們需要多添加幾條文檔數據

 
 
 
  1. db.Quotations.insertMany([
  2.          {name: '黃金標',description: '我黃某人為官一任就得保一方平安',forceValue:Math.random()*100,createDate: '2021-04-11'},
  3.          {name: '賈貴',description: '皇軍沒來的時候,你欺負我,皇軍來了你還欺負我,那皇軍不是TM白來了嗎',forceValue:Math.random()*100,createDate: '2021-04-14'},
  4.          {name: '黑藤',description: '全東亞乃至全亞洲都找不到第二張想你這么一張空前絕后的臉來',forceValue:Math.random()*100,createDate: '2021-04-22'},
  5.          {name: '賈貴',description: '這我哪知道啊,我就知道她長得嘿',forceValue:Math.random()*100, createDate: '2021-04-19'}
  6.      ])

分組使用聚合函數

常用聚合函數

  • $sum
  • $avg
  • $max
  • $min
  • $first
  • $last

根據 name 分組并使用 $sum 函數求和,得到一下結果

  • $match 查詢條件
  • $group 根據字段分組(可以是多字段) + 聚合函數
 
 
 
  1. > db.Quotations.aggregate(
  2. ...     {"$match":{"createDate":{"$gt":"2021-04-07"}}},
  3. ...     {"$group":{"_id":"$name",'forceValue':{"$sum":"$forceValue"}}},
  4. ... )
  5. { "_id" : "賈貴", "forceValue" : 204.22774268609504 }
  6. { "_id" : "黃金標", "forceValue" : 121.14812944012357 }
  7. { "_id" : "黑藤", "forceValue" : 91.2583132098972 }
  8. { "_id" : "孫友福", "forceValue" : 43.71427504449847 }

這相當于在 Mysql 中執(zhí)行

 
 
 
  1. select id,sum(forceValue) from Quotations where createDate > "2021-04-07" group by name;  

$avg 求平均

 
 
 
  1. > db.Quotations.aggregate(
  2. ...     {"$match":{"createDate":{"$gt":"2021-04-07"}}},
  3. ...     {"$group":{"_id":"$name",'forceValue':{"$avg":"$forceValue"}}},
  4. ... )
  5. { "_id" : "賈貴", "forceValue" : 68.07591422869835 }
  6. { "_id" : "黃金標", "forceValue" : 60.574064720061784 }
  7. { "_id" : "黑藤", "forceValue" : 45.6291566049486 }
  8. { "_id" : "孫友福", "forceValue" : 43.71427504449847 }

多字段分組

根據 createDate + name 進行分組之后求和

 
 
 
  1. > db.Quotations.aggregate(
  2. ... ...     {"$match":{"createDate":{"$gt":"2021-04-07"}}},
  3. ... ...     {"$group":{"_id":{"createDate":"$createDate","name":"$name"},'forceValue':{"$sum":"$forceValue"}}},
  4. ... ... )
  5. { "_id" : { "createDate" : "2021-04-11", "name" : "黃金標" }, "forceValue" : 99.26063352194744 }
  6. { "_id" : { "createDate" : "2021-04-16", "name" : "黑藤" }, "forceValue" : 14.796604243632927 }
  7. { "_id" : { "createDate" : "2021-04-17", "name" : "孫友福" }, "forceValue" : 43.71427504449847 }
  8. { "_id" : { "createDate" : "2021-04-18", "name" : "賈貴" }, "forceValue" : 94.18697675337746 }
  9. { "_id" : { "createDate" : "2021-04-26", "name" : "黃金標" }, "forceValue" : 21.887495918176125 }
  10. { "_id" : { "createDate" : "2021-04-14", "name" : "賈貴" }, "forceValue" : 81.9931941785778 }
  11. { "_id" : { "createDate" : "2021-04-22", "name" : "黑藤" }, "forceValue" : 76.46170896626427 }
  12. { "_id" : { "createDate" : "2021-04-19", "name" : "賈貴" }, "forceValue" : 28.04757175413979 }

我不能再繼續(xù)寫下去了,MongoDB 的基礎入門就介紹到這里

更多教程請參考 MongoDB官方文檔

結束語

我是吳老板,以上就是我們這次的干貨分享了。最后重申下:

MongoDB 是非關系型數據庫的代表,一款基于鍵值儲存的高性能數據庫。常作為爬蟲儲存數據庫。

MongoDB 是一個基于分布式文件存儲的數據庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數據存儲解決方案。

MongoDB 是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。


新聞名稱:一文帶你搞懂爬蟲儲存數據庫MongoDB
鏈接分享:http://m.5511xx.com/article/dhccccc.html