欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MongoDB安裝、基礎操作和聚合實例介紹

 更新時間:2024年07月10日 09:36:29   作者:AlfredZhao  
雖然MongoDB這些年很流行,但筆者之前沒研究過,現(xiàn)在有需求研究這類NoSQL的數(shù)據(jù)庫,是為了驗證其是否可被替換,本文給大家介紹MongoDB安裝、基礎操作和聚合實例詳解,感興趣的朋友一起看看吧

雖然MongoDB這些年很流行,但筆者之前沒研究過,現(xiàn)在有需求研究這類NoSQL的數(shù)據(jù)庫,是為了驗證其是否可被替換。

MongoDB是很輕量的文檔數(shù)據(jù)庫,簡單測試也懶得專門準備虛擬機環(huán)境了,直接在macOS上安裝測試下其基礎功能。

  • 1.使用 Homebrew 安裝 MongoDB
  • 2.啟動/停止 MongoDB 服務
  • 3.啟動 MongoDB Shell
  • 4.體驗 MongoDB 基本操作
  • 5.體驗 MongoDB 聚合操作

1. 使用 Homebrew 安裝 MongoDB

# 添加 MongoDB 存儲庫
brew tap mongodb/brew
# 安裝 MongoDB 社區(qū)版
brew install mongodb-community

2. 啟動/停止 MongoDB 服務

# 啟動 MongoDB 服務
brew services start mongodb/brew/mongodb-community
# 停止 MongoDB 服務(這個當然要等我們體驗測試完成后才停..)
brew services stop mongodb/brew/mongodb-community

3. 啟動 MongoDB Shell

# 打開 MongoDB 的交互式 Shell
mongosh

在mongosh登錄到MongoDB時可以看到,筆者這里安裝的是7.0.12版本的MongoDB,使用默認端口27017:

jingyuzhao@jingyuzhao-mac ~ % mongosh
Current Mongosh Log ID:	668ce3d3012a1d349d3a46b3
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.10
Using MongoDB:		7.0.12
Using Mongosh:		2.2.10

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-07-09T15:09:54.021+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test> 

4. 體驗MongoDB基本操作

下面進行一些基本的 MongoDB 操作示例:

1)創(chuàng)建數(shù)據(jù)庫和集合:

-- 切換到要使用的數(shù)據(jù)庫(如果不存在則會自動創(chuàng)建)
use mydb
-- 創(chuàng)建集合
db.createCollection("myCollection")

2)插入文檔:

-- 插入單個文檔:
db.myCollection.insertOne({ name: "Alfred", age: 34 })
-- 插入多個文檔:
db.myCollection.insertMany([
  { name: "Mcdull", age: 33 },
  { name: "Sally", age: 4 }
])

3)查詢文檔:

-- 查詢所有文檔:
db.myCollection.find()
-- 查詢滿足條件的文檔:
db.myCollection.find({ age: { $gt: 25 } })

4)更新文檔:

-- 更新單個文檔:
db.myCollection.updateOne({ name: "Alfred" }, { $set: { age: 29 } })
-- 更新多個文檔:
db.myCollection.updateMany({ age: { $lt: 35 } }, { $set: { status: "Active" } })

5)刪除文檔:

-- 刪除單個文檔,name值為'Sally'的記錄:
db.myCollection.deleteOne({ name: "Sally" })
-- 刪除多個文檔,status值為'Active'的記錄:
db.myCollection.deleteMany({ status: "Active" })

5. 體驗MongoDB聚合操作

1)創(chuàng)建測試用例

-- 刪除 sales 集合
db.sales.drop()
-- 創(chuàng)建 sales 集合并插入示例文檔
db.sales.insertMany([
  {
    "order_id": 1001,
    "product": "Laptop",
    "quantity": 2,
    "unit_price": 1200,
    "customer": "Alice",
    "order_date": ISODate("2024-06-07T08:30:00Z")
  },
  {
    "order_id": 1002,
    "product": "Monitor",
    "quantity": 1,
    "unit_price": 500,
    "customer": "Bob",
    "order_date": ISODate("2024-06-10T10:15:00Z")
  },
  {
    "order_id": 1003,
    "product": "Keyboard",
    "quantity": 3,
    "unit_price": 50,
    "customer": "Alice",
    "order_date": ISODate("2024-06-15T14:45:00Z")
  },
  {
    "order_id": 1004,
    "product": "Mouse",
    "quantity": 5,
    "unit_price": 20,
    "customer": "Charlie",
    "order_date": ISODate("2024-07-09T09:30:00Z")
  }
])

查詢這個集合結果:
db.sales.find()

mydb> db.sales.find()
[
  {
    _id: ObjectId('668cf766749a72317b175646'),
    order_id: 1001,
    product: 'Laptop',
    quantity: 2,
    unit_price: 1200,
    customer: 'Alice',
    order_date: ISODate('2024-06-07T08:30:00.000Z')
  },
  {
    _id: ObjectId('668cf766749a72317b175647'),
    order_id: 1002,
    product: 'Monitor',
    quantity: 1,
    unit_price: 500,
    customer: 'Bob',
    order_date: ISODate('2024-06-10T10:15:00.000Z')
  },
  {
    _id: ObjectId('668cf766749a72317b175648'),
    order_id: 1003,
    product: 'Keyboard',
    quantity: 3,
    unit_price: 50,
    customer: 'Alice',
    order_date: ISODate('2024-06-15T14:45:00.000Z')
  },
  {
    _id: ObjectId('668cf766749a72317b175649'),
    order_id: 1004,
    product: 'Mouse',
    quantity: 5,
    unit_price: 20,
    customer: 'Charlie',
    order_date: ISODate('2024-07-09T09:30:00.000Z')
  }
]
mydb> 

2)執(zhí)行聚合操作

示例 1: 計算每個客戶的總銷售額和訂單數(shù)量

db.sales.aggregate([
  {
    $group: {
      _id: "$customer",
      totalSales: { $sum: { $multiply: ["$quantity", "$unit_price"] } },
      totalOrders: { $sum: 1 }
    }
  },
  { $sort: { totalSales: -1 } } // 按總銷售額降序排序
])

查詢結果:【計算每個客戶的總銷售額和訂單數(shù)量】

[
  { _id: 'Alice', totalSales: 2550, totalOrders: 2 },
  { _id: 'Bob', totalSales: 500, totalOrders: 1 },
  { _id: 'Charlie', totalSales: 100, totalOrders: 1 }
]

示例 2: 查找每種產品的平均銷售價格和銷售數(shù)量

db.sales.aggregate([
  {
    $group: {
      _id: "$product",
      avgPrice: { $avg: "$unit_price" },
      totalQuantity: { $sum: "$quantity" }
    }
  },
  { $sort: { _id: 1 } } // 按產品名稱升序排序
])

查詢結果:【查找每種產品的平均銷售價格和銷售數(shù)量】

[
  { _id: 'Keyboard', avgPrice: 50, totalQuantity: 3 },
  { _id: 'Laptop', avgPrice: 1200, totalQuantity: 2 },
  { _id: 'Monitor', avgPrice: 500, totalQuantity: 1 },
  { _id: 'Mouse', avgPrice: 20, totalQuantity: 5 }
]

示例 3: 篩選特定日期范圍內的銷售訂單并投影字段

db.sales.aggregate([
  {
    $match: {
      order_date: {
        $gte: ISODate("2024-06-01"),
        $lte: ISODate("2024-06-30")
      }
    }
  },
  {
    $project: {
      order_id: 1,
      product: 1,
      quantity: 1,
      totalAmount: { $multiply: ["$quantity", "$unit_price"] }
    }
  }
])

查詢結果:【篩選特定日期范圍內的銷售訂單并投影字段】

[
  {
    _id: ObjectId('668cf766749a72317b175646'),
    order_id: 1001,
    product: 'Laptop',
    quantity: 2,
    totalAmount: 2400
  },
  {
    _id: ObjectId('668cf766749a72317b175647'),
    order_id: 1002,
    product: 'Monitor',
    quantity: 1,
    totalAmount: 500
  },
  {
    _id: ObjectId('668cf766749a72317b175648'),
    order_id: 1003,
    product: 'Keyboard',
    quantity: 3,
    totalAmount: 150
  }
]

至此,我們學習了如何安裝、啟動和停止 MongoDB,并通過 MongoDB Shell 執(zhí)行基礎的 CRUD 操作(創(chuàng)建、查詢、更新和刪除文檔),同時探索了 MongoDB 的聚合操作,用于實現(xiàn)復雜的數(shù)據(jù)分析。后續(xù),會繼續(xù)研究關于Oracle 23ai在JSON這方面的能力表現(xiàn)。

到此這篇關于MongoDB安裝、基礎操作和聚合實例詳解的文章就介紹到這了,更多相關MongoDB安裝內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • mongodb 3.2.5安裝詳細過程

    mongodb 3.2.5安裝詳細過程

    這篇文章主要介紹了mongodb 3.2.5安裝過程詳細記錄,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,感興趣的朋友一起看看吧
    2016-10-10
  • MongoDB中如何使用JOIN操作詳解

    MongoDB中如何使用JOIN操作詳解

    相信大家都知道m(xù)ongodb是不支持join操作的,因此我們只能自己來實現(xiàn)這個功能。所以下面這篇文章主要給大家介紹了關于在MongoDB中如何使用JOIN操作的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友們下面來一起看看吧。
    2017-07-07
  • 高效mongodb的php分頁類(不使用skip)

    高效mongodb的php分頁類(不使用skip)

    這篇文章主要介紹了高效mongodb的php分頁類,并且沒有使用mongodb的skip來實現(xiàn)分頁,需要的朋友可以參考下
    2014-05-05
  • MongoDB使用場景總結

    MongoDB使用場景總結

    這篇文章介紹了什么場景該用MongoDB,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-02-02
  • mongodb字段值自增長實現(xiàn)代碼

    mongodb字段值自增長實現(xiàn)代碼

    這篇文章主要介紹了mongodb字段值自增長實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • MongoDB插入、更新、刪除文檔實現(xiàn)代碼

    MongoDB插入、更新、刪除文檔實現(xiàn)代碼

    本文通過實例代碼給大家簡單介紹了mongodb插入、更新、刪除文檔的方法,需要的的朋友參考下吧
    2017-04-04
  • Laravel?框架中使用?MongoDB?數(shù)據(jù)庫的操作

    Laravel?框架中使用?MongoDB?數(shù)據(jù)庫的操作

    這篇文章主要介紹了Laravel?框架中使用?MongoDB?數(shù)據(jù)庫的問題及操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 為MongoDB數(shù)據(jù)庫注冊windows服務

    為MongoDB數(shù)據(jù)庫注冊windows服務

    這篇文章介紹了為MongoDB數(shù)據(jù)庫注冊windows服務的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • Centos系統(tǒng)搭建MongoDB數(shù)據(jù)庫

    Centos系統(tǒng)搭建MongoDB數(shù)據(jù)庫

    這篇文章介紹了Centos系統(tǒng)搭建MongoDB數(shù)據(jù)庫的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Mongodb啟動報錯完美解決方案:about to fork child process,waiting until server is ready for connections.

    Mongodb啟動報錯完美解決方案:about to fork child pr

    在使用命令行啟動 MongoDB 的時候報錯:about to fork child process, waiting until server is ready for connections.forked process: 50411,造成這個報錯的原因是 “MongoDB” 服務沒有正常的關閉,在終端連接非正常斷開后,再次執(zhí)行 MongoDB 的時候報錯
    2023-04-04

最新評論