https://github.com/Orange-codee/Mongoose-practice
安裝:npm install mongoose
初始化程式碼:
https://mongoosejs.com/docs/index.html
const express = require("express");
const app = express();
const mongoose = require("mongoose");
app.set("view engine", "ejs");
//參考Mongoose官網GettingStart
main().catch((err) => console.log(err));
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/exampleDB");
console.log("成功連結mongoDB");
}
app.listen(3000, () => {
console.log("伺服器正在聆聽 port3000");
});
若要在程式語言中使用或存取 MongoDB,我們需要工具讓資料庫可以跟JavaScript程式碼連結。這類工具的特點就是,能夠將JavaScript中的Object轉換成MongoDB 當中的 document,因此,這類的工具叫做object-document mapping (ODM)。在市面上,眾多MongoDB的ODM當中,最熱門的叫做mongoose。 使用ODM的好處在於:
*. SQL資料庫使用的工具叫做ORM ,而NoSQL 資料庫使用的工具叫做ODM。兩者功能相同但名稱不同。
在Mongoose中,兩個keyword需要記得: