Database

aydenoteยท2022๋…„ 5์›” 15์ผ
0

Node.js

๋ชฉ๋ก ๋ณด๊ธฐ
2/2
post-thumbnail

๐Ÿ“– ์˜ค๋Š˜ ํ•™์Šต ๋ฝ€์ธํŠธ!

  1. NoSQL
  2. mongodb
    2-1 compass
    2-2 mongoose

1. NoSQL

๐Ÿ•ต๏ธโ€โ™€๏ธSQL์ด๋ž€?
๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์กฐํ™”ํ•˜์—ฌ ์ €์žฅ์‹œ์ผœ ๋†“์€ ์ง‘ํ•ฉ์ฒด(Database)์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ฑฐ๋‚˜ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ Query๋ฌธ์„ ์ฒด๊ณ„ํ™” ์‹œ์ผœ๋†“์€ ๊ฒŒ SQL์ด๋‹ค. ํƒ€์ž…์ด ๋ถˆ๋ถ„๋ช…ํ•œ ๋ฐ์ดํ„ฐ, ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ์— ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค.


โœ schema : ์–ด๋–ค ๊ตฌ์กฐ๋กœ ์–ด๋–ค ์กฐ๊ฑด์œผ๋กœ ์ €์žฅ๋˜์–ด์•ผํ•˜๋Š”์ง€ ์ •์˜ํ•œ ๊ฒƒ.
SQL์—๋Š” schema๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋Š”๋ฐ ์ด๋กœ ์ธํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๋ณต ์—†์ด ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๊ณ , ์ˆ˜์ •์ด ๋ฒˆ๊ฑฐ๋กœ์›Œ ์œ ์—ฐํ•˜์ง€ ์•Š๋‹ค๋Š” ๋‹จ์ ๋„ ์žˆ๋‹ค.


๐Ÿ•ต๏ธโ€โ™€๏ธ NoSQL์ด๋ž€?
SQL์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ์–ธ์–ด์ด๋‹ค.


โœ schema๊ฐ€ ์—†๊ณ , JSON ํ˜•ํƒœ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
์žฅ์  : ์œ ์—ฐํ•˜๊ณ  ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
๋‹จ์  : ํƒ€์ž…์ด ๋ถˆ๋ถ„๋ช…ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ์ผ๊ด€์„ฑ์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค.

2. mongodb

๐Ÿ•ต๏ธโ€โ™€๏ธmongodb๋ž€?
NoSQL์˜ ๋Œ€ํ‘œ์ ์ธ DB์ด๋‹ค. schema๊ฐ€ ์—†๊ณ  JSON ํ˜•ํƒœ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์–ด Node.js์™€ ๊ถํ•ฉ์ด ์ข‹๋‹ค.

2-1 compass

๐Ÿ•ต๏ธโ€โ™€๏ธcompass๋ž€?
mongodb๋ฅผ GUI ํ˜•ํƒœ๋กœ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํˆด์ด๋‹ค.

2-2 mongoose

๐Ÿ•ต๏ธโ€โ™€๏ธmongoose๋ž€?
mongodb์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•  ๋•Œ, ํ›จ์”ฌ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํŒจํ‚ค์ง€์ด๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์™€ DB์˜ ๋ฐ์ดํ„ฐ๋ฅผ 1:1๋กœ ๋งค์นญํ•ด์ค€๋‹ค. ์ฆ‰, DB ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.


โœ mongoose ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ
VSC ํ„ฐ๋ฏธ๋„์—์„œ npm install mongoose ์ž…๋ ฅ์œผ๋กœ ์„ค์น˜.

// ./bin/www
const mongoose = require("mongoose");
const db = mongoose.connection;
db.on("error", console.error);
db.once("open", () =>{
  console.log("connected to mongodb server!!")
})
mongoose.connect(
  `mongodb+srv://ayden:<password>@first-project.drox0.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`,
  {useNewUrlParser: true, userUnifiedTopology: true} // ํ•ด๋‹น ์˜ต์…˜์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฒฝ๊ณ  ๋ฐœ์ƒ.
)
profile
๊ธฐ๋กํ•˜๋Š” ๊ฐœ๋ฐœ์ž Ayden ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€