๐ŸŒฑ → ๐ŸŒณ

[MongoDB] mongoDB(์ •์˜, ๊ตฌ์กฐ, ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•) ๋ณธ๋ฌธ

Server/MongoDB

[MongoDB] mongoDB(์ •์˜, ๊ตฌ์กฐ, ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•)

BAY 2022. 9. 24. 11:40
728x90

๐Ÿ“Œ MongoDB(Humongous DB)

์žฅ์  

  • data๋ฅผ ์ต์ˆ™ํ•œ JSON ํ˜•ํƒœ๋กœ ์ฒ˜๋ฆฌ -> ๋น ๋ฅด๊ฒŒ JSON ์ „ํ™˜ ๊ฐ€๋Šฅ
  • DB ๊ตฌ์กฐ์˜ ๋ณ€๊ฒฝ ์šฉ์ด
  • ์ œ์•ฝ์ด ์—†์Œ -> ๋†’์€ ์ˆ˜ํ‰ ํ™•์žฅ์„ฑ, ์Šคํ‚ค๋งˆ ์„ค๊ณ„์˜ ์œ ์—ฐ์„ฑ 

๋‹จ์ 

  • ํ‘œ์ค€์ด ์—†์Œ(= ์ œ์•ฝ์ด ์—†์Œ)
  • ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ์กฐํ™” ๋˜์–ด์žˆ์ง€ ์•Š์Œ
    • ๋‹จ์ˆœํ•œ ๊ตฌ์กฐ์˜ ์ฟผ๋ฆฌ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 
    • ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ ๋ฐ ์•ˆ์ •์„ฑ์„ DB๊ฐ€ ์•„๋‹Œ APP ๋ ˆ๋ฒจ์—์„œ ๊ด€๋ฆฌํ•ด์ค˜์•ผ ํ•จ -> ๋ฒ„๊ทธ ๋ฐœ์ƒ ํ™•๋ฅ  ๋†’์Œ

 

โœ… MongoDB์˜ ๊ตฌ์กฐ

โœ… MongoDB ํŒจํ‚ค์ง€ ์„ค์น˜

mongoDB์šฉ js ํŒŒ์ผ ์ƒ์„ฑ 

routes/mongo.js ํŒŒ์ผ ์ƒ์„ฑ 

npm i mongodb

 

๐Ÿ“ mongo.js ํŒŒ์ผ ์‹คํ–‰ 

node routes/mongo.js

์•„๋ฌด๋Ÿฐ ๋ฉ”์‹œ์ง€๊ฐ€ ์•ˆ๋œจ๋ฉด ์ •์ƒ์ ์œผ๋กœ ์ ‘์†์ด ๋œ ๊ฒƒ 

 

๐Ÿ“ MongoDB ์ ‘์†์šฉ ํ•จ์ˆ˜ ์ƒ์„ฑ 

ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ async ์‚ฌ์šฉ 

์„œ๋ฒ„์—์„œ ํ†ต์‹  ๋ถ€๋ถ„์€ ์ „๋ถ€ await๋กœ ์ฒ˜๋ฆฌ

 

์ ‘์† ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์„œ ์ถœ๋ ฅํ•˜์—ฌ ์ ‘์†์ด ์ž˜ ๋˜๋Š” ์ง€ ํ…Œ์ŠคํŠธ

const { MongoClient, ServerApiVersion } = require('mongodb');

// id, password ๋ถ€๋ถ„ ๋ณธ์ธ๊ฒƒ์œผ๋กœ ์ˆ˜์ •
const uri =
  'mongodb+srv://Id:password@cluster0.sdiakr0.mongodb.net/?retryWrites=true&w=majority';

const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  serverApi: ServerApiVersion.v1,
});

async function main() {
  const db = await client.connect();
  console.log(db);
  
  await client.close();
}

main();

 

๐Ÿ”ซ MongoDB ์ฒซ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ

const users = client.db('db๋ช…').collection('users');

 

๐Ÿ“ users.insertMany : ํ•œ๋ฒˆ์— ๋งŽ์€ ๋ฐ์ดํ„ฐ(document) ์‚ฝ์ž…

await users.insertMany([
    {
      name: 'pororo',
      age: 5,
    },
    {
      name: 'loopy',
      age: 6,
    },
    {
      name: 'crong',
      age: 4,
    },
  ]);

 

๐Ÿ“ users.find({}): ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ๋‹ค์คŒ 

const data = users.find({});

 

 

๐Ÿ“ data.forEach(console.log) ๋Š” ๋ฐ์ดํ„ฐ(Document)์˜ ๊ฐ’์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ถœ๋ ฅํ•ด์คŒ (๋‹จ, ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ด๋ฏ€๋กœ await ํ•„์š”!)
await data.forEach(console.log);

 

์ „์ฒด ์ฝ”๋“œ ํ™•์ธ 

async function main() {
  await client.connect();
  
  const users = client.db('db๋ช…').collection('users');
  
  await users.deleteMany({});
  await users.insertMany([
    {
      name: 'pororo',
      age: 5,
    },
    {
      name: 'loopy',
      age: 6,
    },
    {
      name: 'crong',
      age: 4,
    },
  ]);
  
  const data = users.find({});
  await data.forEach(console.log);
  
  await client.close();
}

 

โœ… mongoDB query

โœ”๏ธ ์‚ฝ์ž…

๐Ÿ“ insertOne : ํ•˜๋‚˜์˜ document ์‚ฝ์ž…

await users.insertOne({
    name: 'pororo',
    age: 5,
  });

 

๐Ÿ“ insertMany: ์—ฌ๋Ÿฌ document ํ•œ๋ฒˆ์— ์‚ฝ์ž… 

์‚ฝ์ž…ํ•  ๋„ํ๋จผํŠธ๋Š” ๋ฐฐ์—ด์— ๋‹ด๊ธด ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•จ 

await users.insertMany([
    {
      name: 'pororo',
      age: 5,
    },
    {
      name: 'loopy',
      age: 6,
    },
    {
      name: 'crong',
      age: 4,
    },
  ]);

 

โœ”๏ธ ์‚ญ์ œ

๐Ÿ“ deleteOne 

: ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์ • ์ฒ˜์Œ์˜ ๋„ํ๋จผํŠธ ํ•˜๋‚˜๋ฅผ ์‚ญ์ œ 

users.deleteOne({
    name: 'crong',
  });

๐Ÿ“ deleteMany

: ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ๋„ํ๋จผํŠธ ์‚ญ์ œ 

await users.deleteMany({
    age: { $gte: 5 },
  });

 

โœ”๏ธ ์ˆ˜์ •

๐Ÿ“ updateOne

: ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์žฅ ์ฒ˜์Œ์˜ ๋„ํ๋จผํฌ ํ•˜๋‚˜ ์ˆ˜์ • 

await users.updateOne(
    {
      name: 'loopy',
    },
    {
      $set: {
        name: '๋ฃจํ”ผ',
      },
    }
  );

๐Ÿ“ updateMany

: ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ๋„ํ๋จผํฌ ํ•˜๋‚˜ ์ˆ˜์ • 

await users.updateMany(
    {
      age: { $gte: 5 },
    },
    {
      $set: {
        name: '5์‚ด ์ด์ƒ',
      },
    }
  );

 

๐Ÿšฉ $set

: mongoDB์˜ ๋„ํ๋จผํŠธ๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉ 

์ˆ˜์ • query์—์„œ ๋„ํ๋จผํŠธ๋ฅผ ์ˆ˜์ • ํ•  ๋•Œ $set: { ์ˆ˜์ •ํ•  ๋‚ด์šฉ } ์œผ๋กœ ์ˆ˜์ •ํ•จ 

 

๐Ÿฃ ๋น„๊ต์‹

๐Ÿฃ ๋…ผ๋ฆฌ์‹

โœ”๏ธ ๊ฒ€์ƒ‰

๐Ÿ“ findOne

: ๊ฒ€์ƒ‰ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์ดˆ์˜ ๋„ํ๋จผํŠธ๋ฅผ ์ฐพ์•„ ์คŒ

์ฐพ์•„์„œ ๊ฐ’์„ ๋ฐ˜ํ™˜ ํ•˜๋ฏ€๋กœ ๋ณ€์ˆ˜์— ๋„ฃ์–ด์„œ ์ฒ˜๋ฆฌ, ๋˜๋Š” ๋ฐ”๋กœ ์ถœ๋ ฅํ•ด์ค˜์•ผํ•จ 

const data = await users.findOne({
    name: 'loopy',
  });
  console.log(data);

๐Ÿ“ find

: ์กฐ๊ฑด์— ๋งž๋Š” ๋„ํ๋จผํŠธ ์ „๋ถ€๋ฅผ ์ฐพ์•„ ์คŒ 

 

โœจ find ํŠน์ง• 

  • find๋กœ ์ฐพ์€ ๊ฐ’์€ return์ด ๋˜๊ธด ํ•˜์ง€๋งŒ mongoDB์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ cursor ๊ฐ์ฒด๋กœ ์ €์žฅ๋จ
    • ์ฆ‰, ํŠน์ • DB๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๋Š” ๊ฒƒ 
  • return์ด ๋  ๋•Œ await๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ 
  • find๋กœ ์ฐพ์€ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด forEach() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•จ 
    • ์ด๋•Œ๋Š” await๋ฅผ ๋ถ™์–ด์ค˜์•ผ๋งŒ ์ถœ๋ ฅ ๊ฐ€๋Šฅ 
  • ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด toArray() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ 
    • ์ด๋•Œ๋„ await ๋ถ™์–ด์ค˜์•ผ ์ถœ๋ ฅ ๊ฐ€๋Šฅ 
const data = users.find({
    name: 'loopy',
  });
  console.log(data);
  await data.forEach(console.log);

 

โœ… user ์„œ๋น„์Šค๋ฅผ mongoDB๋กœ ์ฒ˜๋ฆฌ

mongo.js์—์„œ mongoDB์˜ ์ ‘์† client๋ฅผ ๋ชจ๋“ˆ๋กœ exportsํ•ด์„œ ๋‹ค๋ฅธ ๋ผ์šฐํ„ฐ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๊ธฐ

// mongo.js
const { MongoClient, ServerApiVersion } = require('mongodb');

const uri =
  'mongodb+srv://tetz:qwer1234@cluster0.sdiakr0.mongodb.net/?retryWrites=true&w=majority';

const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  serverApi: ServerApiVersion.v1,
});

module.exports = client;

 

๐Ÿ“ mongoDB ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ & ์‚ฌ์šฉํ•˜๊ธฐ 

users ๋ผ์šฐํ„ฐ์—์„œ mongoDB ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ 

// users.js
const mongoClient = require('./mongo');

 

getUsers ํ•จ์ˆ˜๋กœ mongoDB์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ 

async function getUsers() {
  const client = await mongoClient.connect();
  const cursor = client.db('db๋ช…').collection('users').find();
  const data = await cursor.toArray();
  return data;
}

 

updateUsers ํ•จ์ˆ˜๋กœ mongoDB ๋‚ด์šฉ์„ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ

async function updateUsers(data) {
  const client = await mongoClient.connect();
  const cursor = client.db('db๋ช…').collection('users');
  await cursor.deleteMany({});
  await cursor.insertMany(data);
}

(์‚ฌ์‹ค, ๋” ๋””ํ…Œ์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜์ง€๋งŒ ์ผ๋‹จ ๋‹ค๋ฃจ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ž‘์œผ๋ฏ€๋กœ ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌ)

 

๐Ÿ“db ํ†ต์‹  ์ฒ˜๋ฆฌ 

user ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ ์˜ค๋Š” ๊ณณ ์•„๋ž˜ ์ฝ”๋“œ ์ž‘์„ฑํ•ด์„œ user ๋ฐ์ดํ„ฐ๋ฅผ db์—์„œ ๋ฐ›์•„์˜ค๋„๋ก ์ฒ˜๋ฆฌ

const USER = await getUsers();

 

user ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ ๋˜๊ณ  ๋‚œ ํ›„์—๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ์„œ db์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ ์ €์žฅ 

await updateUsers(USER);

 

728x90

'Server > MongoDB' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[mongoDB] Database์— ์ž๋ฃŒ ์ €์žฅํ•˜๋Š” ๋ฒ•  (0) 2022.08.27
[mongoDB] mongoDB ์…‹ํŒ…ํ•˜๊ธฐ  (0) 2022.08.27