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

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2] 8์žฅ. MongoDB

23-24/Node.js 2

by _๋„๋‹ด 2023. 11. 24. 10:00

๋ณธ๋ฌธ

728x90

 

 

๐ŸŒŸ8์žฅ ํ‚ค์›Œ๋“œ๐ŸŒŸ

Nosql vs. SQL

MongoDB

mongoose

 


NoSQL vs. SQL

RDB์—์„œ ์‚ฌ์šฉํ•˜๋Š” SQL ์™ธ์˜ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ NoSQL์ด๋ผ๊ณ  ํ•œ๋‹ค.

์•„๋ž˜ ํ‘œ๋Š” 

 

 

๋ชฝ๊ณ ๋””๋น„ ์‚ฌ์šฉํ•˜๊ธฐ

MongoDB Community Server ๋‹ค์šด๋กœ๋“œ https://www.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

MongoDB ์…ธ ๋‹ค์šด๋กœ๋“œ https://www.mongodb.com/try/download/shell
์„ค์น˜๊ฒฝ๋กœ C:\Users\USER\AppData\Local\Programs\mongosh\

 

Try MongoDB Tools - Download Free Here

Free download for MongoDB tools to do more with your database. MongoDB Shell, Compass, CLI for Cloud, BI Connector and other database tools available.

www.mongodb.com

 

 

๋ชฝ๊ณ ๋””๋น„ ํฌํŠธ๋Š” 27017๋ฒˆ

  • ๋ชฝ๊ณ ๋””๋น„ ์‹คํ–‰: mongod --ipv6
    ๋ชฝ๊ณ ๋””๋น„ ์ ‘์†: mongosh
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ์ž ๊ณ„์ • ๋งŒ๋“ค๊ธฐ: use admin ํ›„ db.createUser({ user: '์ด๋ฆ„', pwd: '๋น„๋ฐ€๋ฒˆํ˜ธ', roles: ['root'] })
  • ๋ชฝ๊ณ ๋””๋น„ ๋กœ๊ทธ์ธ์„ ์š”๊ตฌํ•˜๋Š” ์‹คํ–‰ mongod --ipv6 --auth
    ๋กœ๊ทธ์ธ mongosh admin -u [์ด๋ฆ„] -p [๋น„๋ฐ€๋ฒˆํ˜ธ]

 

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

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ use [๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…]
    ๋”ฐ๋กœ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก ๋ณด๊ธฐ show dbs
    ๋ฐ์ดํ„ฐ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋งŒ ๋ณด์ธ๋‹ค.
  • ์‚ฌ์šฉ ์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ™•์ธ db

๋ชฝ๊ณ ๋””๋น„์˜ '์ปฌ๋ ‰์…˜' = mysql์˜ 'ํ…Œ์ด๋ธ”'

  • ์ปฌ๋ ‰์…˜ ์ƒ์„ฑ: db.createCollection('users')
    ์ด ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋‹คํ๋จผํŠธ๋ฅผ ๋„ฃ๋Š” ์ˆœ๊ฐ„ ์ปฌ๋ ‰์…˜๋„ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

 

 

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

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

  • ๋ชฝ๊ณ ๋””๋น„๋Š” js ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ž๋ฃŒํ˜•์„ ๋”ฐ๋ฆ„.
  • Date ๋˜๋Š” ์ •๊ทœํ‘œํ˜„์‹ ๊ฐ™์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ ์ž๋ฃŒํ˜•์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์ถ”๊ฐ€์ ์ธ ์ž๋ฃŒํ˜•: Binary Data, ObjectId, Int, Long, Decimal, Timestamp, JavaScript ๋“ฑ

Create(์ƒ์„ฑ)

  • ์ƒ์„ฑ:  db.users.insertOne({name: 'yuz', age: 23, married: false, comment: '์•ˆ๋…•ํ•˜์„ธ์š”, ์ฝ”๋„ˆ์ž…๋‹ˆ๋‹ค.', createAt: new Date ()});

Read(์กฐํšŒ)

  • ๋‹จ๊ฑด ์กฐํšŒ: db.users.find({name: 'yuz'}, {_id: 1})
  • ์ปฌ๋ ‰์…˜ ๋‚ด์˜ ๋ชจ๋“  ๋‹คํ๋จผํŠธ ์กฐํšŒ: db.์ปฌ๋ ‰์…˜๋ช….find({});
  • ์กฐํšŒ ์‹œ ์กฐ๊ฑด ์ฃผ๊ธฐ: db.users.find({age: {$gt: 30}, married: true}, {_id: 0, name: 1, age: 1});
    ๋‘ ๋ฒˆ์งธ ์ธ์ž์— ํ‘œ์‹œ
  • ์ž์ฃผ ์“ฐ์ด๋Š” ์—ฐ์‚ฐ์ž: $gt(์ดˆ๊ณผ), $gte(์ด์ƒ), $lt(๋ฏธ๋งŒ), $lte(์ดํ•˜), $ne(๊ฐ™์ง€ ์•Š์Œ), $or(๋˜๋Š”), $in(๋ฐฐ์—ด ์š”์†Œ ์ค‘ ํ•˜๋‚˜)
  • ์ •๋ ฌ db.users.find(๊ฒ€์ƒ‰์กฐ๊ฑด).sort({age: -1}) -1์€ ๋‚ด๋ฆผ์ฐจ์ˆœ, 1์€ ์˜ค๋ฆ„์ฐจ์ˆœ
  • ์กฐํšŒ๊ฐœ์ˆ˜ ์ œํ•œ limit, ๊ฑด๋„ˆ๋›ฐ๊ธฐ skip: db.users.find(๊ฒ€์ƒ‰์กฐ๊ฑด).sort(์ •๋ ฌ์กฐ๊ฑด).limit(1).skip(1)

Update(์ˆ˜์ •)

  • db.users.updateOne({๊ฒ€์ƒ‰์กฐ๊ฑด}, {์ˆ˜์ •ํ•  ๊ฐ’});
  • ํ•œ ๋‹คํ๋จผํŠธ์˜ ํŠน์ • ํ•„๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ ค๋ฉด db.users.updateOne({๊ฒ€์ƒ‰์กฐ๊ฑด}, {$set: {์ˆ˜์ •ํ•  ํ•„๋“œ: ๊ฐ’} });
  • ์˜ˆ: { $set: {comment: '์•ˆ๋…•ํ•˜์„ธ์š”, ์ด ํ•„๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!'} }

Delete(์‚ญ์ œ)

  • ๋‹จ๊ฑด ์‚ญ์ œ db.users.deleteOne({name: 'nero'})
  • ์—ฌ๋Ÿฌ ๊ฑด ์‚ญ์ œ ์‹œ deleteMany ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

 

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

MySQL์— ์‹œํ€„๋ผ์ด์ฆˆ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋ชฝ๊ณ ๋””๋น„์—๋Š” ๋ชฝ๊ตฌ์Šค(mongoose)๊ฐ€ ์žˆ๋‹ค

  • ODM(Object Document Mapping)
  • ๋ชฝ๊ณ ๋””๋น„์— ์—†๋Š” ์Šคํ‚ค๋งˆ ์ œ๊ณต
  • ๊ตฌ์กฐ์ƒ ์ž์œ ๋กœ์›€์˜ ์œ„ํ—˜์„ฑ: ๋ชฝ๊ณ ๋””๋น„์—๋Š” ํ…Œ์ด๋ธ”์ด ์—†์–ด์„œ ์ž์œ ๋กญ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹ค์ˆ˜๋กœ ์ž˜๋ชป๋œ ์ž๋ฃŒํ˜•์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋‹ค๋ฅธ ๋‹คํ๋จผํŠธ์—๋Š” ์—†๋Š” ํ•„๋“œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜๋„ ์žˆ์Œ

๋ชฝ๊ตฌ์Šค ํŒจํ‚ค์ง€ ์„ค์น˜ npm i mongoose

๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐ ์ฃผ์†Œ ํ˜•์‹: mongodb://[username:password@]localhost[:port][/[database][?options]]
์˜ˆ: mongodb://YUZ:0000@localhost:27017/admin

 

// schemas/index.js
const mongoose = require('mongoose');

const connect = () => {
  if (process.env.NODE_ENV !== 'production') {
    mongoose.set('debug', true);
  }
  mongoose.connect('mongodb://YUZ:0000@localhost:27017/admin', {
    dbName: 'nodejs',
    useNewUrlParser: true,
  }).then(() => {
    console.log("๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐ ์„ฑ๊ณต");
  }).catch((err) => {
    console.error("๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐ ์—๋Ÿฌ", err);
  });
};

mongoose.connection.on('error', (error) => {
  console.error('๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐ ์—๋Ÿฌ', error);
});
mongoose.connection.on('disconnected', () => {
  console.error('๋ชฝ๊ณ ๋””๋น„ ์—ฐ๊ฒฐ์ด ๋Š๊ฒผ์Šต๋‹ˆ๋‹ค. ์—ฐ๊ฒฐ์„ ์žฌ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.');
  connect();
});

module.exports = connect;

 

 

์Šคํ‚ค๋งˆ ์ •์˜ํ•˜๊ธฐ

// schemas/user.js
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);
// schemas/comment.js
const mongoose = require('mongoose');

const { Schema } = mongoose;
const { Types: { ObjectId } } = Schema;
const commentSchema = new Schema({
  commenter: {
    type: ObjectId,
    required: true,
    ref: 'User',
  },
  comment: {
    type: String,
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

module.exports = mongoose.model('Comment', commentSchema);

 

 


๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ (๋นˆ์นธ์„ ๋“œ๋ž˜๊ทธํ•ด์„œ ์ •๋‹ต์„ ๋งžํ˜€ ๋ณด์„ธ์š”!)

 

1.  MySQL์˜ ํ…Œ์ด๋ธ”, ๋กœ์šฐ, ์ปฌ๋Ÿผ์„ ๋ชฝ๊ณ ๋””๋น„์—์„œ๋Š” ๊ฐ๊ฐ (์ปฌ๋ ‰์…˜, ๋‹คํ๋จผํŠธ, ํ•„๋“œ)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

2. ๋ชฝ๊ณ ๋””๋น„์—๋Š” (์Šคํ‚ค๋งˆ)๊ฐ€ ์—†์–ด ํ•„๋“œ ๋„๋ฉ”์ธ ๊ด€๋ฆฌ์— ์œ ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋ชฝ๊ตฌ์Šค์—๋Š” ๊ทธ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

3. ์‹œํ€„๋ผ์ด์ฆˆ๋Š” ORM(Object Relational Mapping), ๋ชฝ๊ตฌ์Šค๋Š” (ODM, Object Document Mapping)์ด๋‹ค.

4. ๋ชฝ๊ตฌ์Šค ๋‹คํ๋จผํŠธ CRUD ๋ฉ”์„œ๋“œ ์ด๋ฆ„: ์ƒ์„ฑ(insertOne), ์กฐํšŒ(find, findOne. findMany), ์ˆ˜์ •(updateOne, updateMany), ์‚ญ์ œ(deleteOne, deleteMany)

5. ์กฐํšŒ ์กฐ๊ฑด์„ ์„ค์ •ํ•  ๋•Œ ์—ฐ์‚ฐ์ž ์•ž์— ๋ถ™๋Š” ๊ธฐํ˜ธ๋Š” ($)์ด๋‹ค. (gt, gte, ne, or ๋“ฑ)

6. ํŠน์ • ๋‹คํ๋จผํŠธ์˜ ํ•„๋“œ ํ•˜๋‚˜๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ: updateOne({๊ฒ€์ƒ‰์กฐ๊ฑด}, {$set: {์ˆ˜์ •ํ•  ํ•„๋“œ: ๊ฐ’}})

 

 


Node.js #2 

Editor : ์œ ์ฆˆ

728x90

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