SQL 與 MongoDB 指令與術語對照關係
術語與概念對照
以下是常見的 SQL 與 MongoDB 術語對照,包含中文和英文的對應說明:
| SQL 術語/概念 | MongoDB 術語/概念 |
|---|---|
| 資料庫 (database) | 資料庫 (database) |
| 資料表 (table) | 集合 (collection) |
| 資料列 (row) | 文件 (document) 或 BSON 文件 |
| 欄位 (column) | 欄位 (field) |
| 索引 (index) | 索引 (index) |
| 資料表聯結 (table joins) | $lookup 嵌入式文件 (embedded documents) |
| 主鍵 (primary key) | _id(自動設為主鍵) |
聚合 (aggregation)(例如 GROUP BY) | aggregation pipeline |
SELECT INTO NEW_TABLE | $out |
MERGE INTO TABLE | $merge |
UNION ALL | $unionWith |
| transactions | transactions |
可執行檔對照
資料庫的可執行檔是用來啟動資料庫伺服器或與資料庫互動的工具。以下是不同資料庫的伺服器和客戶端可執行檔對應:
| MongoDB | MySQL | Oracle | Informix (IDS) | DB2 | |
|---|---|---|---|---|---|
| 伺服器 (Database Server) | mongod | mysqld | oracle | IDS | DB2 Server |
| 客戶端 (Database Client) | mongosh | mysql | sqlplus | DB-Access | DB2 Client |
- 伺服器 (Database Server):如
mongod,是用來啟動 MongoDB 資料庫伺服器的可執行檔,類似於 MySQL 的mysqld。 - 客戶端 (Database Client):如
mongosh,是一個命令列工具,用來與 MongoDB 資料庫互動,執行查詢及管理操作。
查詢資料 (SELECT 與 find) 指令對照
MongoDB 的 find 方法與 SQL 的 SELECT 類似,用來從集合中查詢文件。MongoDB 提供了強大的查詢功能,可以結合查詢操作符(如 $gt, $eq)來篩選結果。
相關連結:
查詢所有資料
-- SQL
SELECT * FROM people;
// MongoDB
db.people.find();
說明:這會查詢 people 表/集合中的所有資料。
查詢特定欄位
-- SQL
SELECT user_id, status FROM people;
// MongoDB
db.people.find({}, { user_id: 1, status: 1 });
說明:只查詢 user_id 和 status 欄位。
使用條件查詢
-- SQL
SELECT * FROM people WHERE age > 30;
// MongoDB
db.people.find({ age: { $gt: 30 } });
說明:查詢所有 age 大於 30 的記錄。在 MongoDB 中,$gt 表示「大於」。
查詢特定條件的資料
-- SQL
SELECT * FROM people WHERE status = 'A';
// MongoDB
db.people.find({ status: 'A' });
說明:查詢 status 為 'A' 的所有資料。
計算資料筆數
-- SQL
SELECT COUNT(*) FROM people;
// MongoDB
db.people.countDocuments();
說明:計算 people collection 中的 document 數量。
插入資料 (INSERT 與 insertOne) 指令對照
MongoDB 使用 insertOne 和 insertMany 來插入文件到集合中,與 SQL 不同的是,MongoDB 不需要事先定義資料結構,_id 自動作為主鍵生成。。
相關連結:
插入新資料
-- SQL
INSERT INTO people (user_id, age) VALUES ('abc123', 55);
// MongoDB
db.people.insertOne({ user_id: 'abc123', age: 55 });
說明:在 MongoDB 中,插入文件時會自動生成 _id 作為主鍵。
更新資料 (UPDATE 與 updateMany) 指令對照
MongoDB 使用 updateOne 或 updateMany 來更新文件,並透過 $set 來修改欄位的值,或是使用 $inc 來遞增數值。
更新特定欄位
-- SQL
UPDATE people SET age = 30 WHERE user_id = 'abc123';
// MongoDB
db.people.updateMany({ user_id: 'abc123' }, { $set: { age: 30 } });
說明:更新所有 user_id 為 'abc123' 的文件,將 age 設為 30。
刪除資料 (DELETE 與 deleteMany) 指令對照
在 MongoDB 中,deleteOne 和 deleteMany 用來刪除符合條件的文件,與 SQL 的 DELETE 操作相似。
相關連結:
刪除符合條件的資料
-- SQL
DELETE FROM people WHERE status = 'A';
// MongoDB
db.people.deleteMany({ status: 'A' });
說明:刪除所有 status 為 'A' 的文件。
刪除所有資料
-- SQL
DELETE FROM people;
// MongoDB
db.people.deleteMany({});
說明:刪除 people 集合中的所有文件。
聚合查詢與資料轉換指令對照
在 SQL 中,GROUP BY、UNION ALL 等聚合查詢是常見的操作。MongoDB 則使用 Aggregation Pipeline 來實現複雜的資料處理,透過管道方式逐步處理文件,最終輸出結果。
| SQL 操作 | MongoDB 聚合操作 |
|---|---|
| 群組(GROUP BY) | $group |
| 聯集(UNION ALL) | $unionWith |
| 篩選條件(HAVING) | $match(在 $group 之後使用) |
| 排序(ORDER BY) | $sort |
| 限制筆數(LIMIT) | $limit |
| 跳過筆數(OFFSET) | $skip |
| 選擇至新表格(SELECT INTO NEW_TABLE) | $out |
| 合併 至表格(MERGE INTO TABLE) | $merge |
| 計算總和(SUM) | $sum |
| 計算平均值(AVG) | $avg |
| 計算筆數(COUNT) | $count |
| 取得最小值(MIN) | $min |
| 取得最大值(MAX) | $max |
| 唯一值(DISTINCT) | $group 或 $addToSet |
以下是一些常見 SQL 與 MongoDB 聚合查詢的範例
計算總數
-- SQL
SELECT COUNT(*) AS count FROM orders;
// MongoDB
db.orders.aggregate([
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
]);
說明:計算 orders 集合中的文件總數。
計算欄位總和
-- SQL
SELECT SUM(price) AS total FROM orders;
// MongoDB
db.orders.aggregate([
{
$group: {
_id: null,
total: { $sum: "$price" }
}
}
]);
說明:計算 orders 集合中 price 欄位的總和。
分組計算總和
-- SQL
SELECT cust_id, SUM(price) AS total FROM orders GROUP BY cust_id;
// MongoDB
db.orders.aggregate([
{
$group: {
_id: "$cust_id",
total: { $sum: "$price" }
}
}
]);
說明:根據 cust_id 分組,計算每個客戶的 price 總和。
分組計算並排序
-- SQL
SELECT cust_id, SUM(price) AS total FROM orders GROUP BY cust_id ORDER BY total;
// MongoDB
db.orders.aggregate([
{
$group: {
_id: "$cust_id",
total: { $sum: "$price" }
}
},
{
$sort: { total: 1 }
}
]);
說明:根據 cust_id 分組計算 price 總和,並按總和排序。