๐ฅ [MongoDB] Node.js ์ ์ฌ์ฉํ๊ธฐ ์ํ MongoDB ์ค์น ๋ฐ ์ค์
Node.js ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด MongoDB ๋ฅผ ์ค์น ๋ฐ ์ค์ ์๋ ํฌ์คํธ์ ๋๋ค.
Database ์ ์ข ๋ฅ
๊ด๊ณํ๊ณผ ๋น ๊ด๊ณํ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ, ์กฐํ, ๊ด๋ฆฌํ๋ ๋ฐฉ์์์ ์ข ๋ฅ๋ฅผ ๋๋ ์ ์๋ค.
๋ฐ์ดํฐ ๋ชจ๋ธ๋ง, ์ ํ๋ฆฌ์ผ์ด์ ์ ์๊ตฌ ์ฌํญ, ์ฑ๋ฅ ๊ณ ๋ ค ์ฌํญ์ ๊ฒฐ์
๊ด๊ณํ DB
MySQL
,Oracle
,SQLite
๋น ๊ด๊ณํ DB
MongoDB
,Redis
,Hadoop
MongoDB ์ ํน์ง
- ๋น ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Document-oriented Database (๋ฌธ์์งํฅ ๋ฐ์ดํฐ๋ฒ ์ด์ค)
- Document (๋ฐ์ดํฐ ๊ตฌ์กฐ๊ฐ key , value ์ ํ๋ ์ด์์ ์์ผ๋ก ๊ตฌ์ฑ, ๋ชจ๋ ๋ฐ์ดํฐ๊ฐ JSON ํํ)
- Collection (Document ์ ๊ทธ๋ฃน ๋จ์๋ก ์ด๋ฃจ์ด์ง)
์ผ๋ก ํฌ๊ฒ ํน์ง์ 4 ๊ฐ์ง๋ก ๋ณผ ์ ์๋ค.
MongoDB ์ค์น
MongoDB
๋ฅผ ์ค์น๋ฅผ terminal
๋ก ํธํ๊ฒ ํ๊ธฐ ์ํด ์ฐ์ ์ ์ผ๋ก Chocolatey
๋ฅผ ์ค์น ๋จผ์ ํ๋ค.
Chocolatey ์ค์น
Linux
์ Brew
์ฒ๋ผ ํธํ๊ฒ ๋ชจ๋์ ์ค์น ํ ์ ์๋ ํด Chocolatey
๋ฅผ ์ค์น ํ๋ค.
1
Get-ExecutionPolicy
๋ฅผ ๋จผ์ ์คํ ํด Restricted
๋ชจ๋์ธ์ง ํ์ธํ๋ค.
๋ง์ฝ Restricted
์ผ ๊ฒฝ์ฐ
1
2
3
4
Set-ExecutionPolicy AllSigned
# or
Set-ExecutionPolicy Bypass-Scope Process
# ๋ฅผ ์ฌ์ฉํด ๋ชจ๋๋ฅผ ๋ณ๊ฒฝํด์ค๋ค.
1
2
3
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# ์ ์ฌ์ฉํด chocolatey ๋ฅผ ์ค์นํด์ค๋ค!
1
choco-?
์ ์ฌ์ฉํด ๋ฒ์ ์ด ๋์จ๋ค๋ฉด ์ ์์ ์ผ๋ก ์ค์น๊ฐ ๋ ๊ฒ์ด๋ค.
๋ง์ฝ ์ค์น๊ฐ ์ ์์ ์ผ๋ก ๋์ง ์๋๋ค๋ฉด Powershell ์ ๊ด๋ฆฌ์ ๊ถํ์ผ๋ก ์คํ์์ผ ๋ค์ ์งํํ๋ค.
MongoDB ์ค์น
๋ฐฉ๊ธ ์ค์นํ Chocolatey
๋ฅผ ์ฌ์ฉํด ์ฝ๊ณ ๊ฐํธํ๊ฒ MongoDB
๋ชจ๋์ ์ฐพ์ ์ ์๋ค.
(์ฐ์ธก์ choco install mongodb ๊ฐ์ด ์
๋ ฅ ๊ฐ์ด๋ค.)
ํด๋น ๊ฐ์ powershell
์ ์
๋ ฅํ๋ฉด
์ถ๊ฐ์ฌํญ ์ ํ ์ฌ๋ถ๋ฅผ ๋ฌผ์ด๋ณด๋๋ฐ, A
๋ฅผ ๋๋ฌ ์ ์ฒด ๋์๋ฅผ ํ๋ค.
(์ ์์ ์ผ๋ก ์ค์น ๋ ๋ชจ์ต)
ํ๊ฒฝ๋ณ์ ์ค์
์ค์น๋ฅผ ๋ง์ณค์ ๊ฒฝ์ฐ ํ๊ฒฝ๋ณ์ Path
๋ฅผ ์ค์ ํด์ฃผ์ด์ผ ํ๋ค.
๋ฐ๋ผ์ ๋ด ์ปดํจํฐ ๋ด MongoDB
์ bin ํด๋ ์์น๋ฅผ ํ์ธํด์ผ ํ๋ค.
์์ฑ์๋ C:\Program Files\MongoDB\Server\7.2\bin
์์ผ๋ฏ๋ก
ํ๊ฒฝ๋ณ์๋ฅผ ์ถ๊ฐํด์ค๋ค.
(mongod ๋ฅผ ์
๋ ฅํด ํด๋น ํ๋ฉด์ฒ๋ผ ๋์ค๋ฉด ์ ์์ ์ผ๋ก ์ค์น ์๋ฃ)
1
2
3
mongosh
# mongodb ์คํ
# ํด๋น ๋ช
๋ น์ด๋ก mongodb ๋ 27017 port-number ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ ์ ์์
Mongoose ๋?
Mongoose
๋ Schema
๋ผ๋ ๊ฒ์ ์ ๊ณตํ๋๋ฐ, Schema
๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์ฝ๊ฒ ๋์์ฃผ๋ ๊ตฌ์กฐ๋ผ๊ณ ๋ณด๋ฉด ๋๋ค.
๋ฐ๋ผ์ ํด๋น ๊ธฐ๋ฅ์ ํตํด Node.js
์์ ๋ฐ์ดํฐ๋ฅผ ์กฐ๊ธ ๋ ํธ๋ฆฌํ๊ฒ ๋ค๋ฃฐ ์ ์๊ฒ ๋์์ค
Schema ์ ์ข ๋ฅ
- String
- Number
- Date
- Boolean
- Buffer
- Mixed (๋ชจ๋ ๋ฐ์ดํฐ ํ์ ๊ฐ๋ฅ, likes Anyโฆ)
- ObjectedId (๊ฐ์ฒด ID)
- Array
Mongoose ์ค์น ๋ฐ ์ฐ๊ฒฐ
์ ์์ ์ผ๋ก MongoDB
์ค์น๋ฅผ ๋ง์ณค์ผ๋ฉด ์ด์ ์ฌ์ฉํ ํ๊ฒฝ์ MongoDB
๋ฅผ ์ฐ๊ฒฐํด ์ค
Mongoose
๋ฅผ ์ค์นํด์ค๋ค.
1
npm install mongoose
๊ทธ ํ localhost:27017
๋ก ์ ์ํด ๋ณด๋ฉด
์ ์์ ์ผ๋ก ์ค์น๋ ๋์์ง๋ง, ๋ธ๋ผ์ฐ์ ๋ ์ ๊ทผํ ์ ์๋ค๋ ๋ฉ์ธ์ง๊ฐ ๋ํ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import mongoose from "mongoose";
// mongoose connect
mongoose
.connect("mongodb://127.0.0.1:27017")
.then(() => {
console.log("mongoDB connection success...");
})
.catch((error) => console.error(error));
// mongoose set
const { Schema } = mongoose;
const WritingSchema = new Schema({
title: String,
contents: String,
dates: {
type: Date,
default: Date.now,
},
}); // Schema ๋ฅผ ์ค์
const Writing = mongoose.model("Writing", WritingSchema);
// Writing์ Writing ์ด๋ผ๋ ์ด๋ฆ์ผ๋ก WritingSchema ์ ํํ๋ฅผ ๋๋ค๊ณ ์ง์
์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ Node.js
์ server ํ์ผ์ ํด๋น ๊ฐ์ ์ถ๊ฐํด ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ Schema ํ์์ ์ง์ ํด์ฃผ๊ณ
Mongoose
๋ฅผ ํตํด Browser
์ mongoDB
๋ฅผ ์ฐ๊ฒฐ ํด์ค๋ค.
์ ์์ ์ผ๋ก ์ฐ๊ฒฐ ๋ ๊ฒ์ ํฐ๋ฏธ๋์์ ํ์ธ ํ ํ
MongoDB
์ Schema
๋ฅผ ์ถ๊ฐํด ์ค๋ค.
show dbs
> ํ์ฌ ์กด์ฌํ๋ database ๋ค์ ์ถ๋ ฅuse express
> express ๋ผ๋ ์ด๋ฆ์ผ๋ก database ๋ฅผ ์ ํ. ์์ผ๋ฉด ์์ฑ๋ ํจdb.createCollection("writings")
> writings ๋ผ๋ collection ์ ์์ฑ
show collections
> ํ์ฌ ์ ํ ๋ db ์ ์กด์ฌํ๋ collection ๋ชฉ๋ก ์ถ๋ ฅdb.writings.insert(...)
> db ์ wiritings collection ์ ๊ฐ์ ์ถ๊ฐdb.writings.find().pretty()
> ํฌํจ ๋ object ๊ฐ๋ค์ ์ถ๋ ฅ
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
app.post("/write", async (req, res) => {
const title = req.body.title;
const contents = req.body.contents;
// const date = req.body.date;
// mongodb ์ ์ฅ
const writing = new Writing({
title: title,
contents: contents,
});
const result = await writing
.save()
.then(() => {
console.log("Writing Success...");
res.render("detail", {
detail: { title: title, contents: contents, date: date },
});
})
.catch((error) => {
console.error(error);
res.render("write");
// ์คํจํ์ ๋
});
// db ์ ๊ฐ์ ๋ฃ๋ ๊ฑฐ๋ ์ค๋๊ฑธ๋ฆฌ๋ฏ๋ก ๋น๋๊ธฐ๋ก ๊ฑธ์ด์ฃผ์ด์ผ ํจ
// ๋ง๋ writing ์ ์ ์ฅ์์ผ์ค๋ค.
});