๐Ÿ—„๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๐ŸŒฟ ์ค‘๊ธ‰

MongoDB ์‹ค์ „

NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค MongoDB ์™„์ „ ์ •๋ณต

โฑ๏ธ 6์‹œ๊ฐ„ ๐Ÿ‘๏ธ 21 ์กฐํšŒ
๐Ÿ“– ๊ฐ•์ขŒ ๋‚ด์šฉ
# MongoDB ์‹ค์ „ ๊ฐ€์ด๋“œ

## ํ•™์Šต ๋ชฉํ‘œ
NoSQL ๋ฌธ์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค MongoDB๋ฅผ ๋งˆ์Šคํ„ฐํ•ฉ๋‹ˆ๋‹ค.

## 1. MongoDB๋ž€?
๋ฌธ์„œ ๊ธฐ๋ฐ˜ NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ž…๋‹ˆ๋‹ค.
- JSON ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅ
- ์Šคํ‚ค๋งˆ ์œ ์—ฐ์„ฑ
- ์ˆ˜ํ‰ ํ™•์žฅ ์šฉ์ด
- ํ’๋ถ€ํ•œ ์ฟผ๋ฆฌ ๊ธฐ๋Šฅ

## 2. ์„ค์น˜ ๋ฐ ์ ‘์†
Docker๋กœ ์‹คํ–‰:
docker run -d -p 27017:27017 --name mongo mongo

์ ‘์†:
mongosh
mongosh mongodb://localhost:27017/mydb

## 3. ๊ธฐ๋ณธ ๋ช…๋ น์–ด
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค:
show dbs
use mydb
db.dropDatabase()

์ปฌ๋ ‰์…˜:
show collections
db.createCollection("users")
db.users.drop()

## 4. CRUD ๊ธฐ๋ณธ
์‚ฝ์ž…:
db.users.insertOne({name: "Kim", age: 25})
db.users.insertMany([{name: "Lee"}, {name: "Park"}])

์กฐํšŒ:
db.users.find()
db.users.find({name: "Kim"})
db.users.findOne({name: "Kim"})
db.users.find({age: {$gt: 20}})

์ˆ˜์ •:
db.users.updateOne({name: "Kim"}, {$set: {age: 26}})
db.users.updateMany({}, {$inc: {age: 1}})

์‚ญ์ œ:
db.users.deleteOne({name: "Kim"})
db.users.deleteMany({age: {$lt: 18}})

## 5. ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž
๋น„๊ต:
$eq, $ne - ๊ฐ™์Œ, ๋‹ค๋ฆ„
$gt, $gte - ์ดˆ๊ณผ, ์ด์ƒ
$lt, $lte - ๋ฏธ๋งŒ, ์ดํ•˜
$in, $nin - ํฌํ•จ, ๋ฏธํฌํ•จ

๋…ผ๋ฆฌ:
$and, $or, $not, $nor

์˜ˆ์‹œ:
db.users.find({age: {$gte: 20, $lte: 30}})
db.users.find({$or: [{name: "Kim"}, {age: 25}]})

## 6. ํ”„๋กœ์ ์…˜๊ณผ ์ •๋ ฌ
ํ•„๋“œ ์„ ํƒ:
db.users.find({}, {name: 1, age: 1, _id: 0})

์ •๋ ฌ:
db.users.find().sort({age: -1})

์ œํ•œ:
db.users.find().limit(10).skip(20)

## 7. ์ธ๋ฑ์Šค
์ƒ์„ฑ:
db.users.createIndex({email: 1})
db.users.createIndex({name: 1, age: -1})
db.users.createIndex({location: "2dsphere"})

์กฐํšŒ:
db.users.getIndexes()

์‚ญ์ œ:
db.users.dropIndex("email_1")

## 8. Aggregation
db.orders.aggregate([
{$match: {status: "completed"}},
{$group: {_id: "$userId", total: {$sum: "$amount"}}},
{$sort: {total: -1}},
{$limit: 10}
])

์ฃผ์š” ์Šคํ…Œ์ด์ง€:
$match - ํ•„ํ„ฐ๋ง
$group - ๊ทธ๋ฃนํ™”
$sort - ์ •๋ ฌ
$project - ํ•„๋“œ ์„ ํƒ
$lookup - JOIN
$unwind - ๋ฐฐ์—ด ํŽผ์น˜๊ธฐ

## 9. Mongoose (Node.js)
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/mydb");

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

const User = mongoose.model("User", userSchema);

const user = new User({name: "Kim", email: "kim@test.com"});
await user.save();

const users = await User.find({age: {$gte: 20}}).sort({name: 1});

## 10. ๊ด€๊ณ„ ์„ค๊ณ„
์ž„๋ฒ ๋”ฉ (1:Few):
{
name: "Kim",
addresses: [
{city: "Seoul", zip: "12345"},
{city: "Busan", zip: "67890"}
]
}

์ฐธ์กฐ (1:Many):
// users
{_id: ObjectId("..."), name: "Kim"}

// posts
{_id: ObjectId("..."), title: "Hello", userId: ObjectId("...")}

Populate:
const post = await Post.findById(id).populate("userId");

## 11. ๋ฐฑ์—…๊ณผ ๋ณต์›
๋ฐฑ์—…:
mongodump --db mydb --out /backup/

๋ณต์›:
mongorestore --db mydb /backup/mydb/

## 12. ์„ฑ๋Šฅ ์ตœ์ ํ™”
์ฟผ๋ฆฌ ๋ถ„์„:
db.users.find({name: "Kim"}).explain("executionStats")

์ธ๋ฑ์Šค ํ™œ์šฉ:
- ์ž์ฃผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ•„๋“œ์— ์ธ๋ฑ์Šค
- ๋ณตํ•ฉ ์ธ๋ฑ์Šค ์ˆœ์„œ ๊ณ ๋ ค
- ์ปค๋ฒ„๋ง ์ธ๋ฑ์Šค ํ™œ์šฉ

์ œํ•œ:
- ํ•„์š”ํ•œ ํ•„๋“œ๋งŒ ์กฐํšŒ
- limit ์‚ฌ์šฉ
- ์ ์ ˆํ•œ ๋ฐฐ์น˜ ํฌ๊ธฐ

## ์ถ”์ฒœ ํ•™์Šต ์ˆœ์„œ
1. CRUD ๊ธฐ๋ณธ ์ตํžˆ๊ธฐ
2. ์ฟผ๋ฆฌ ์—ฐ์‚ฐ์ž ํ™œ์šฉ
3. Aggregation ํŒŒ์ดํ”„๋ผ์ธ
4. Mongoose ์—ฐ๋™
5. ์ธ๋ฑ์Šค์™€ ์„ฑ๋Šฅ ์ตœ์ ํ™”