MongoDB Compass 圖形介面操作資料庫

先前我們使用具體的操作範例比較 Oracle DB JSON 與 MongoDB 的差異,請看 Oracle DB JSON 與 MongoDB 的差異。而這篇要來補充說明,怎麼用 MongoDB Compass 圖形介面操作資料庫。

MongoDB Compass

MongoDB Compass 是官方提供免費的圖形使用者介面(GUI)工具,用來操作和分析 MongoDB 資料,通過圖形介面執行查詢,並可在 macOS、Windows 和 Linux 操作系統上運行。

好用功能:

  • 瀏覽資料: Compass 介面上查看和瀏覽 MongoDB 文件資料,並可同時連接到不同的資料庫。
  • 命令行介面(CLI): 內建一個 MongoDB Shell,可以直接輸入命令操作資料庫。
  • 匯入匯出資料: 可將 JSON 或 CSV 檔案中的資料匯入 MongoDB,也可以將資料匯出。
  • AI 查詢: 可使用自然語言來簡化資料查詢。
  • 更多功能: …

下載 Compass

下載 MongoDB Compass

MongoDB Compass 不支援虛擬桌面環境喔。

設定資料庫

註冊帳號

先去 註冊 MongoDB Atlas 帳號。

建立專案

資料結構

在建立專案前,我們先了解在 MongoDB 中的資料結構的層級。

1
2
3
4
5
6
Project
└── Cluster
└── Database
└── Collection [Documents]
└── Document {name: 'Ned Stark', name: 'Robert Baratheon', ...}
└── Field {name: 'Ned Stark'}

個別對應到 RDBMS 中的概念:

MongoDB RDBMS
Project 專案
Cluster 資料庫伺服器
Database 資料庫
Collection 表 (Table)
Document 行 (Row)
Field 欄位 (Column)

建立專案

註冊完成並 登入帳號 後,就可以建立專案。
mongodb-compass-1

為專案命名後,點擊「Next」。
mongodb-compass-2

建立集群

接著,我們要建立集群(Cluster),點擊「Create」。
mongodb-compass-3

選擇「M0」免費方案,提供 512 MB 的儲存空間。
mongodb-compass-4

  • 為集群命名
  • 勾選「Automate Security setup」自動新增目前的 IP 啟用本地連線。
  • 勾選「Preload sample dataset」預載 45MB 的範例資料集。
  • 選擇「Google Cloud」,地區為台灣。
  • 選項確認沒問題後,就可以點擊「Create Deployment」。

mongodb-compass-5

點擊「Copy」複製密碼後,才能點擊「Create Database User」。
mongodb-compass-7

這是資料庫帳號密碼要記得保存下來。

點擊「Choose a connection method」。
mongodb-compass-6

MongoDB 有提供很多種連線方式,我們這次選擇「Compass」。
mongodb-compass-8

因為我已經先安裝好 MongoDB Compass,所以可直接點擊「I have MongoDB Compass installed」。如果還沒安裝可以點擊「I don't have MongoDB Compass installed」進行安裝。

複製「連線字串(connection string)」。
mongodb-compass-9

連接資料庫

打開 MongoDB Compass,建立新連線,將剛才複製的連線字串貼上。
mongodb-compass-10

當出現綠色連線標誌時,表示連線成功!剛才預載的範例資料集位於 sample_mflix。
mongodb-compass-10-1

建立資料

我將以簡單電商網站的資料作為的 MongoDB 資料庫設計範例,包括用戶、商品、訂單等資料結構。

建立資料庫

點擊「cluser-2」後,再點擊「Open MongoDB shell」。
mongodb-compass-11-0

在「Open MongoDB shell」的視窗中,輸入以下指令。

1
2
// 創建並切換到 ecommerce 資料庫
use ecommerce

mongodb-compass-11

mongodb-compass-12

建立集合

在「Open MongoDB shell」的視窗中,輸入以下指令插入資料。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 創建 users 集合並插入資料
db.createCollection('users')
db.users.insertOne({
"name": "John Doe",
"email": "john.doe@example.com",
"passwordHash": "hashedpassword",
"phone": "+123456789",
"addresses": [
{
"type": "home",
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001",
"country": "USA"
},
{
"type": "work",
"street": "456 Office St",
"city": "New York",
"state": "NY",
"zip": "10002",
"country": "USA"
}
],
"createdAt": new Date("2024-01-01T12:00:00Z"),
"isAdmin": false
})

// 創建 products 集合並插入資料
db.createCollection('products')
db.products.insertOne({
"name": "Smartphone X",
"description": "Latest model of the Smartphone X series with 128GB storage.",
"category": "Electronics",
"price": 799.99,
"stock": 50,
"brand": "Brand X",
"specifications": {
"color": "Black",
"weight": "180g",
"batteryLife": "10h"
},
"images": [
"https://example.com/images/smartphone-x-front.jpg",
"https://example.com/images/smartphone-x-back.jpg"
],
"ratings": {
"average": 4.5,
"totalReviews": 150
},
"createdAt": new Date("2024-01-05T08:30:00Z")
})

// 創建 orders 集合並插入資料
db.createCollection('orders')
db.orders.insertOne({
"userId": ObjectId("64a543d293be0307c812b73f"),
"items": [
{
"productId": ObjectId("64a543d293be0307c812b741"),
"quantity": 2,
"price": 799.99
}
],
"totalAmount": 1599.98,
"shippingAddress": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001",
"country": "USA"
},
"orderStatus": "pending",
"orderDate": new Date("2024-01-10T12:30:00Z"),
"shippingDate": new Date("2024-01-11T12:30:00Z"),
"deliveryDate": new Date("2024-01-15T14:00:00Z")
})

創建索引

在「Open MongoDB shell」的視窗中,輸入以下指令創建索引。

1
2
// 為 users 集合的 email 欄位創建唯一索引
db.users.createIndex({ email: 1 }, { unique: true })

執行查詢

命令行

在「Open MongoDB shell」的視窗中,輸入以下指令查詢資料。

1
2
// 查找訂單狀態為"pending"的訂單
db.orders.find({ "orderStatus": "pending" })

mongodb-compass-13

自然語言

MongoDB Compass 支援使用自然語言生成查詢。(有AI還需我嗎XD)
mongodb-compass-16

mongodb-compass-14

例如,輸入「查找訂單狀態為 pending 的訂單」,Compass 會自動生成相應的查詢語法。
mongodb-compass-15

總結

MongoDB Compass 是一個非常好用的圖形化工具,使用上很直覺,再加上有了自然語言生成指令,省去很多人工寫查詢語法的時間,在資料庫管理與操作上更能提高工作效率。