์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2ํŒ€] #9. ๋ชฝ๊ณ ๋””๋น„

24-25/Node.js 2

by sksmsyena 2024. 12. 27. 10:00

๋ณธ๋ฌธ

728x90

                         ๐ŸŒŸํ‚ค์›Œ๋“œ: ๋ชฝ๊ณ ๋””๋น„, ์ปดํผ์Šค, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, CRUD, ๋ชฝ๊ตฌ์Šค

 


 

1. ๋ชฝ๊ณ ๋””๋น„

- mongoDB : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ• ์‚ฌ์šฉ

- NoSQL vs SQL

mongoDB๋Š” NoSQL์˜ ๋Œ€ํ‘œ์ ์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

 

NoSQL:

- ๊ณ ์ •๋œ ํ…Œ์ด๋ธ”์ด ์—†๋‹ค. ์ปฌ๋Ÿผ์„ ๋”ฐ๋กœ ์ •์˜ํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ users ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ค๊ณ  ๋์ด๋‹ค. users์ปฌ๋ ‰์…˜์—๋Š” ์–ด๋– ํ•œ ๋ฐ์ดํ„ฐ๋“  ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

- JOIN ๊ธฐ๋Šฅ์ด ์—†์œผ๋ฉฐ, ํ•˜๋‚˜์˜ ์ฟผ๋ฆฌ๋กœ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ํ•ฉ์น˜๋Š” ์ž‘์—…์ด ํ•ญ์ƒ ๊ฐ€๋Šฅํ•˜์ง€๋Š” ์•Š๋‹ค. ๋™์‹œ์— ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ์ฟผ๋ฆฌ๊ฐ€ ์„ž์—ฌ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.

→ But, ๋ชฝ๊ณ ๋””๋น„๋Š” ํ™•์žฅ์„ฑ๊ณผ ๊ฐ€์šฉ์„ฑ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•œ๋‹ค. 

๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ๋ณด์žฅํ•ด์ฃผ๋Š” ๋Œ€์‹  ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๊ณ , ์‰ฝ๊ฒŒ ์—ฌ๋Ÿฌ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

Example) ํ•ต์‹ฌ๊ธฐ๋Šฅ ์™ธ์˜ ๋น…๋ฐ์ดํ„ฐ, ๋ฉ”์‹œ์ง•, ์„ธ์…˜ ๊ด€๋ฆฌ ๋“ฑ์—๋Š” ํ™•์žฅ์„ฑ๊ณผ ๊ฐ€์šฉ์„ฑ ์œ„ํ•ด ๋ชฝ๊ณ ๋””๋น„ ์‚ฌ์šฉํ•œ๋‹ค.

 

MySQL:

- users ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค ๋•Œ name, age ๋“ฑ์˜ ์ปฌ๋Ÿผ๊ณผ ์ž๋ฃŒํ˜•, ์˜ต์…˜ ๋“ฑ์„ ์ •์˜ํ•œ๋‹ค.

Example) ํ•ญ๊ณต์‚ฌ ์˜ˆ์•ฝ ์‹œ์Šคํ…œ์˜ ๊ฒฝ์šฐ ๋น„ํ–‰๊ธฐ ํ‘œ์— ๊ด€ํ•œ ์ •๋ณด๊ฐ€ ๋ชจ๋“  ํ•ญ๊ณต์‚ฌ์— ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์ „๋‹ฌ๋˜์–ด์•ผํ•˜๋ฏ€๋กœ ์ฃผ๋กœ ์˜ˆ์•ฝ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์‚ฌ์šฉํ•œ๋‹ค.

 

 

2. ๋ชฝ๊ณ ๋””๋น„ ์„ค์น˜ํ•˜๊ธฐ

โ€ป ์šด์˜์ฒด์ œ์— ๋”ฐ๋ผ ์ฑ…์„ ์ฐธ๊ณ ํ•ด ์„ค์น˜

https://mongodb.com/try/download/community

 

Try MongoDB Community Edition

Try MongoDB Community Edition on premise non-relational database including the Community Server and Community Kubernetes Operator for your next big project!

www.mongodb.com

์ด ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€์„œ ๋‚ด๋ ค๋ฐ›์œผ๋ฉด ๋œ๋‹ค.

 

 

3. ์ปดํผ์Šค ์„ค์น˜ํ•˜๊ธฐ

- ๋ชฝ๊ณ ๋””๋น„๋Š” ๊ด€๋ฆฌ ๋„๊ตฌ๋กœ ์ปดํผ์Šค(compass)๋ฅผ ์ œ๊ณต

 

https://mongodb.com/download-center/compass

์ด ์‚ฌ์ดํŠธ ๋“ค์–ด๊ฐ€์„œ ๋‹ค์šด๋ฐ›์œผ๋ฉด ๋œ๋‹ค.

 

- ์ปดํผ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด GUI๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌํ•˜๋ฏ€๋กœ ์ด์šฉํ•œ๋‹ค.

 

 

4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ ์ปฌ๋ ‰์…˜ ์ƒ์„ฑํ•˜๊ธฐ

๋ชฝ๊ณ ๋””๋น„ ํ”„๋กฌํ”„ํŠธ์— ์ ‘์†ํ•œ ํ›„ ์ง„ํ–‰ํ•œ๋‹ค.

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด๋Š” use [๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…] ์ด๋‹ค.

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก์„ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋Š” show dbs ์ด๋‹ค.

 

5. CRUD ์ž‘์—…ํ•˜๊ธฐ

5-1. CREATE(์ƒ์„ฑ)

- ์ปฌ๋ ‰์…˜์—๋Š” ์•„๋ฌด ๋ฐ์ดํ„ฐ๋‚˜ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

- ๋ชฝ๊ณ ๋””๋น„์˜ ์ž๋ฃŒํ˜•:

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ• ์‚ฌ์šฉํ•œ๋‹ค. 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ž๋ฃŒํ˜•์„ ๋”ฐ๋ฅธ๋‹ค. Date, Binary Data, ObjectId, Int, Long, Decimal, Timestamp, JavaScript ๋“ฑ์˜ ์ž๋ฃŒํ˜•์ด ์žˆ๋‹ค. 

Undefined์™€ Symbol์€ ๋ชฝ๊ณ ๋””๋น„์—์„œ ์ž๋ฃŒํ˜•์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

ObjectId๋Š” MySQL์—์„œ ๊ธฐ๋ณธ ํ‚ค๋กœ ์“ฐ์ด๋Š” ๊ฐ’๊ณผ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. -> ๊ณ ์œ ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ๋‹คํ๋จผํŠธ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

- db.์ปฌ๋ ‰์…˜๋ช….insertOne(๋‹คํ๋จผํŠธ)๋กœ ๋‹คํ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์ฒ˜๋Ÿผ ์ƒ์„ฑํ•œ๋‹ค. 

 

5-2. Read(์กฐํšŒ)

- find({})๋Š” ์ปฌ๋ ‰์…˜ ๋‚ด์˜ ๋ชจ๋“  ๋‹คํ๋จผํŠธ๋ฅผ ์กฐํšŒํ•œ๋‹ค.

- ํŠน์ • ํ•„๋“œ๋งŒ ์กฐํšŒํ•˜๋Š” ๊ฒฝ์šฐ

nodejs> db.users.find({}, { _id: 0, name: 1, married: 1 });
[
  { "name" : "zero", "married" : false },
  { "name" : "nero", "married" : true }
]

 

- $gt : ์‹œํ€„๋ผ์ด์ฆˆ์˜ ์ฟผ๋ฆฌ์ด๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ช…๋ น์–ด ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

→ ๊ทธ ์™ธ ๋ฉ”์†Œ๋“œ:

 $gt(์ดˆ๊ณผ), $gte(์ด์ƒ), $lt(๋ฏธ๋งŒ), $lte(์ดํ•˜), $ne(๊ฐ™์ง€ ์•Š์Œ), $or(๋˜๋Š”), $in(๋ฐฐ์—ด ์š”์†Œ ์ค‘ ํ•˜๋‚˜), $or(OR์—ฐ์‚ฐ), sort ๋ฉ”์„œ๋“œ(์ •๋ ฌ,  -1 ์€ ๋‚ด๋ฆผ์ฐจ์ˆœ, 1์€ ์˜ค๋ฆ„์ฐจ์ˆœ), limit ๋ฉ”์„œ๋“œ( ์กฐํšŒํ•  ๋‹คํ๋จผํŠธ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •), skip ๋ฉ”์„œ๋“œ( ๋‹คํ๋จผํŠธ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด์„œ ๋ช‡ ๊ฐœ๋ฅผ ๊ฑด๋„ˆ๋›ธ์ง€ ์„ค์ •)

 

5-3. Update(์ˆ˜์ •)

nodejs> db.users.updateOne({ name: 'nero' }, { $set: { comment: '์•ˆ๋…•ํ•˜์„ธ์š” ์ด ํ•„๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!' } });
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}

 

์ฒซ ๋ฒˆ์งธ ๊ฐ์ฒด๋Š” ์ˆ˜์ •ํ•  ๋‹คํ๋จผํŠธ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฐ์ฒด์ด๊ณ , ๋‘ ๋ฒˆ์งธ ๊ฐ์ฒด๋Š” ์ˆ˜์ •ํ•  ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค. $set: ์–ด๋–ค ํ•„๋“œ๋ฅผ ์ˆ˜์ •ํ• ์ง€ ์ •ํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค. ์ผ๋ถ€ ํ•„๋“œ๋งŒ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ $set ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

5-4. Delete(์‚ญ์ œ)

nodejs> db.users.deleteOne({ name: 'nero' })
{ acknowledged: true, deletedCount: 1 }

 

 

6. ๋ชฝ๊ตฌ์Šค ์‚ฌ์šฉํ•˜๊ธฐ

- ๋ชฝ๊ตฌ์Šค๋Š” ์‹œํ€„๋ผ์ด์ฆˆ์™€ ๋‹ฌ๋ฆฌ ODM(Object Document Mapping)์ด๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. ๋‹คํ๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ  ODM์ด๋‹ค.

- ๋ชฝ๊ณ ๋””๋น„์— ์—†์–ด์„œ ๋ถˆํŽธํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๋ชฝ๊ตฌ์Šค๊ฐ€ ๋ณด์™„ํ•œ๋‹ค. -> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์™€ ๋งคํ•‘ํ•˜๋Š” ์ด์œ ์ด๋‹ค.

- ๋ชฝ๊ตฌ์Šค๋Š” ๋ชฝ๊ณ ๋””๋น„์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ธฐ ์ „์— ๋…ธ๋“œ ์„œ๋ฒ„ ๋‹จ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ํ•„ํ„ฐ๋งํ•œ๋‹ค.

populate๋ผ๋Š” ๋ฉ”์„œ๋“œ : ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๊ฐ€์ ธ์˜จ๋‹ค. ES2015 ํ”„๋กœ๋ฏธ์Šค ๋ฌธ๋ฒ•๊ณผ ๊ฐ•๋ ฅํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์€ ์ฟผ๋ฆฌ ๋นŒ๋”๋ฅผ ์ง€์›ํ•œ๋‹ค.

 

6-1. ๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐํ•˜๊ธฐ

๋ชฝ๊ณ ๋””๋น„๋Š” ์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ•ด ์—ฐ๊ฒฐํ•œ๋‹ค. ์ฃผ์†Œ ํ˜•์‹์€ mongodb://[username:password@]host[:port][/[database][?options]]์ด๋‹ค. [ ] ๋ถ€๋ถ„์€ ์žˆ์–ด๋„ ๋˜๊ณ  ์—†์–ด๋„ ๋œ๋‹ค. username๊ณผ password์— ๋ชฝ๊ณ ๋””๋น„ ๊ณ„์ • ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋„ฃ๋Š”๋‹ค.

์ฃผ์†Œ ์˜ˆ์‹œ:

mongodb://์ด๋ฆ„:๋น„๋ฐ€๋ฒˆํ˜ธ@localhost:27017/admin

 

6-2. ์Šคํ‚ค๋งˆ ์ •์˜ํ•˜๊ธฐ

const mongoose = require('mongoose');

const { Schema } = mongoose;
const userSchema = new Schema({
  name: {
    type: String,
    required: true,
    unique: true,
  },
  age: {
    type: Number,
    required: true,
  },
  married: {
    type: Boolean,
    required: true,
  },
  comment: String,
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

module.exports = mongoose.model('User', userSchema);

 

- ๋ชฝ๊ตฌ์Šค ๋ชจ๋“ˆ์—์„œ Schema ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์Šคํ‚ค๋งˆ๋ฅผ ๋งŒ๋“ ๋‹ค. . ํ•„๋“œ๋ฅผ ๊ฐ๊ฐ ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

- ๋ชฝ๊ตฌ์Šค๋Š” ์•Œ์•„์„œ _id๋ฅผ ๊ธฐ๋ณธ ํ‚ค๋กœ ์ƒ์„ฑํ•˜๋ฏ€๋กœ _id ํ•„๋“œ๋Š” ์ ์–ด์ค„ ํ•„์š”๊ฐ€ ์—†๊ณ , ๋‚˜๋จธ์ง€ ํ•„๋“œ์˜ ์ŠคํŽ™๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

- name ํ•„๋“œ์˜ ์ž๋ฃŒํ˜•์€ String์ด๊ณ  ํ•„์ˆ˜์ด๋ฉฐ ๊ณ ์œ ํ•œ ๊ฐ’์ดํ•˜๊ณ ,  age ํ•„๋“œ๋Š” Number ์ž๋ฃŒํ˜•์ด๊ณ  ํ•„์ˆ˜์ด๋ฉฐ, married ํ•„๋“œ๋Š” ๋ถˆ ๊ฐ’ ์ž๋ฃŒํ˜•์ด๊ณ  ํ•„์ˆ˜์ด๋‹ค. comment ํ•„๋“œ๋Š” String ์ž๋ฃŒํ˜•์ด๊ณ , required๋‚˜ default ๋“ฑ์˜ ์˜ต์…˜์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž๋ฃŒํ˜•๋งŒ ๋ช…์‹œํ•˜๋ฉด ๋œ๋‹ค. createdAt ํ•„๋“œ๋Š” Date ์ž๋ฃŒํ˜•์ด๊ณ  ๊ธฐ๋ณธ๊ฐ’์€ Date.now(๋ฐ์ดํ„ฐ ์ƒ์„ฑ ๋‹น์‹œ์˜ ์‹œ๊ฐ„)์ด๋‹ค.

๋งˆ์ง€๋ง‰์—๋Š” ๋ชฝ๊ตฌ์Šค์˜ model ๋ฉ”์„œ๋“œ๋กœ ์Šคํ‚ค๋งˆ์™€ ๋ชฝ๊ณ ๋””๋น„ ์ปฌ๋ ‰์…˜์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ ๋‹ค.

 

6-3. ์ฟผ๋ฆฌ ์ˆ˜ํ–‰ํ•˜๊ธฐ

 

- GET /users์™€ POST /users ์ฃผ์†Œ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ์˜ ๋ผ์šฐํ„ฐ์ด๋‹ค. ๊ฐ๊ฐ ์‚ฌ์šฉ์ž๋ฅผ ์กฐํšŒํ•˜๋Š” ์š”์ฒญ๊ณผ ์‚ฌ์šฉ์ž๋ฅผ ๋“ฑ๋กํ•˜๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. GET /์—์„œ๋„ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ–ˆ์ง€๋งŒ GET /users์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

- ์‚ฌ์šฉ์ž๋ฅผ ๋“ฑ๋กํ•  ๋•Œ๋Š” ๋จผ์ € ๋ชจ๋ธ .create ๋ฉ”์„œ๋“œ๋กœ ์ €์žฅํ•œ๋‹ค. ์ •์˜ํ•œ ์Šคํ‚ค๋งˆ์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์—ˆ์„ ๋•Œ๋Š” ๋ชฝ๊ตฌ์Šค๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ ,  _id๋Š” ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

GET /users/:id/comments ๋ผ์šฐํ„ฐ๋Š” ๋Œ“๊ธ€ ๋‹คํ๋จผํŠธ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ผ์šฐํ„ฐ์ด๋‹ค.  Comment ์Šคํ‚ค๋งˆ commenter ํ•„๋“œ์˜ ref๊ฐ€ User๋กœ ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ์ž๋™์œผ๋กœ users ์ปฌ๋ ‰์…˜์—์„œ ์‚ฌ์šฉ์ž ๋‹คํ๋จผํŠธ๋ฅผ ์ฐพ์•„ ํ•ฉ์นœ๋‹ค. commenter ํ•„๋“œ๊ฐ€ ์‚ฌ์šฉ์ž ๋‹คํ๋จผํŠธ๋กœ ์น˜ํ™˜๋œ๋‹ค. ์ด์ œ commenter ํ•„๋“œ๋Š” ObjectId๊ฐ€ ์•„๋‹ˆ๋ผ ๊ทธ ObjectId๋ฅผ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž ๋‹คํ๋จผํŠธ๊ฐ€ ๋œ๋‹ค.

 

const express = require('express');
const Comment = require('../schemas/comment');

const router = express.Router();

router.post('/', async (req, res, next) => {
  try {
    const comment = await Comment.create({
      commenter: req.body.id,
      comment: req.body.comment,
    });
    console.log(comment);
    const result = await Comment.populate(comment, { path: 'commenter' });
    res.status(201).json(result);
  } catch (err) {
    console.error(err);
    next(err);
  }
});

router.route('/:id')
  .patch(async (req, res, next) => {
    try {
      const result = await Comment.update({
        _id: req.params.id,
      }, {
        comment: req.body.comment,
      });
      res.json(result);
    } catch (err) {
      console.error(err);
      next(err);
    }
  })
  .delete(async (req, res, next) => {
    try {
      const result = await Comment.remove({ _id: req.params.id });
      res.json(result);
    } catch (err) {
      console.error(err);
      next(err);
    }
  });

module.exports = router;

 

์ฝ˜์†”

$ npm start
> learn-mongoose@0.0.1 start 
> nodemon app

[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
3002 ๋ฒˆ ํฌํŠธ์—์„œ ๋Œ€๊ธฐ ์ค‘
๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐ ์„ฑ๊ณต
Mongoose: users.createIndex({ name: 1 }, { unique: true, background: true })

→ ์„œ๋ฒ„ ์‹คํ–‰

 

 

 


Quiz

1. NoSQL์˜ ๋Œ€ํ‘œ์  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ (      )๋ผ ํ•œ๋‹ค.

2. ๋ชฝ๊ณ ๋””๋น„์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋„๊ตฌ๋Š” (          )์ด๋‹ค.

3. ๋ชฝ๊ณ ๋””๋น„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ 4๊ฐ€์ง€ ์ž‘์—…์„ (    )๋ผ๊ณ  ํ•œ๋‹ค. 

4. ๋ชฝ๊ตฌ์Šค๋Š” ์‹œํ€„๋ผ์ด์ฆˆ์™€ ๋‹ฌ๋ฆฌ (         )๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค.

5. ๋ชฝ๊ณ ๋””๋น„์— ์—†์–ด ๋ชฝ๊ตฌ์Šค๊ฐ€ ๋ณด์™„ํ•˜๋Š”๋ฐ ์ด๋Š” (    )์™€ ๋งคํ•‘ํ•˜๋Š” ์ด์œ ๋‹ค.

6. ๋ชฝ๊ณ ๋””๋น„๋Š” (    )๋ฅผ ์‚ฌ์šฉํ•ด ์—ฐ๊ฒฐํ•œ๋‹ค.

7. ๋ชฝ๊ตฌ์Šค๋Š” (   )๋ฅผ ๊ธฐ๋ณธํ‚ค๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

Programming Quiz

8. Update ์ฝ”๋“œ์ด๋‹ค. ๋นˆ์นธ์„ ์ฑ„์šฐ์‹œ์˜ค.

nodejs> (  ).(    ).(    )({ name: 'nero' }, { (    ): { comment: '์•ˆ๋…•ํ•˜์„ธ์š” ์ด ํ•„๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!' } });
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}

 

9. name, married ์ด ๋‘ ๊ฐœ์˜ ํŠน์ • ํ•„๋“œ๋งŒ ์กฐํšŒํ•˜๋Š” (READ) ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 


Answer

1. ๋ชฝ๊ณ ๋””๋น„

2. ์ปดํผ์Šค

3. CRUD

4. ODM

5. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด

6. ์ฃผ์†Œ

7. _id

8.

nodejs> db.users.updateOne({ name: 'nero' }, { $set: { comment: '์•ˆ๋…•ํ•˜์„ธ์š” ์ด ํ•„๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!' } });
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}

 

9.

nodejs> db.users.find({}, { _id: 0, name: 1, married: 1 });
[
  { "name" : "zero", "married" : false },
  { "name" : "nero", "married" : true }
]

 


 

์ถœ์ฒ˜) ์กฐํ˜„์ •, ใ€ŒNode.js ๊ต๊ณผ์„œ ๊ฐœ์ • 3ํŒใ€, ๊ธธ๋ฒ—(2022), 8์žฅ

 

Corner node.js 2ํŒ€

Editor : Eunki

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ