系列: 不要再說你不會後端!30 天 Node.js 降維打擊指南
前情提要: 在前面的章節中,我們學會了 RESTful API 設計、Middleware 機制與 Request 資料驗證。但目前所有的資料都是存在記憶體中,伺服器一重啟資料就消失了。今天我們要正式引入持久化儲存的利器——MongoDB 與 Mongoose!
對於 Node.js 開發者來說,MongoDB 是最親切且主流的資料庫選擇:
| 關聯式資料庫 (RDBMS / SQL) | MongoDB (NoSQL) | 說明 |
|---|---|---|
| Database | Database | 資料庫 |
| Table | Collection | 資料集合(如 users) |
| Row / Record | Document | 單筆 JSON 文件資料 |
| Column | Field | 資料欄位(鍵值對) |
雖然 MongoDB 本身彈性很大,但在實際團隊開發中,如果完全沒有資料規範,資料庫很快就會變成無序的垃圾場。
Mongoose 是 Node.js 最熱門的 ODM (Object Document Mapper) 工具,它幫我們提供了:
find(), create(), findById())來操作資料庫。npm install mongoose
config/db.jsconst mongoose = require('mongoose');
const connectDB = async () => {
try {
// 預設本機 MongoDB 連線字串 (可替換為 MongoDB Atlas 雲端網址)
const conn = await mongoose.connect('mongodb://localhost:27017/my_app');
console.log(`MongoDB 連線成功: ${conn.connection.host}`);
} catch (error) {
console.error(`MongoDB 連線失敗: ${error.message}`);
// 連線失敗時終步程序
process.exit(1);
}
};
module.exports = connectDB;
在 Mongoose 中,標準流程是:先定義 Schema(資料規格)➔ 再建立 Model(操作介面)。
models/User.jsconst mongoose = require('mongoose');
// 1. 定義 Schema
const userSchema = new mongoose.Schema(
{
username: {
type: String,
required: [true, '使用者名稱為必填欄位'],
trim: true,
minlength: [3, '名稱最少需要 3 個字元']
},
email: {
type: String,
required: [true, 'Email 為必填欄位'],
unique: true, // 確保 Email 不會重複
lowercase: true
},
age: {
type: Number,
default: 18
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user'
}
},
{
timestamps: true // 自動新增 createdAt 與 updatedAt 欄位
}
);
// 2. 建立並匯出 Model (Mongoose 會自動將 User 轉為小寫複數的集合名稱 'users')
const User = mongoose.model('User', userSchema);
module.exports = User;
最後,我們在 app.js 中啟動資料庫連線並測試寫入資料:
const express = require('express');
const connectDB = require('./config/db');
const User = require('./models/User');
const app = express();
app.use(express.json());
// 啟動資料庫連線
connectDB();
// 測試新增使用者的 API
app.post('/api/users', async (req, res) => {
try {
const newUser = await User.create(req.body);
res.status(201).json({
success: true,
data: newUser
});
} catch (error) {
res.status(400).json({
success: false,
message: error.message
});
}
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
[!IMPORTANT]
- Schema 定義結構,Model 負責操作:Schema 是藍圖,Model 是實際拿來呼叫資料庫 API 的元件。
unique: true不是驗證器:它是 MongoDB 建立索引的方式,要避免重複寫入建議搭配應用層的驗證。- **善用
timestamps: true**:可以在 Schema 設定中開啟自動維護建立與更新時間欄位。