# 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. ์ธ๋ฑ์ค์ ์ฑ๋ฅ ์ต์ ํ