#19 MongoDB
#19.0 Introduction
NOSQL_Not Only SQL, ๋ฐ์ดํฐ ์ ์ฅ ๋ฐฉ์์ด ๋ค๋ฆ, MongoDB, Redis, Neo...
MongoDB - JSON ๋ฌธ์ ํ์ฉ, ๋น๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค
#19.1 Installation
#19.2 Creating Documents
Easy, ๋ฐ์ดํฐ ํ์์ด๋ ์ ์ฝ์กฐ๊ฑด, ๊ตฌ์กฐ ๋ฑ SQL์ ์ ์ฝ์ด ์์ > ์ค์์ ๋ฏผ๊ฐํด์ง
cls - clear
#19.3 Reading Documents
db.movies.find({director:"Christopher Nolan"})
db.movies.find({rating: { $gte: 8 }})
// $gte ๊ฐ๊ฑฐ๋ ํฐ
db.movies.find({ year: { $gt: 2000, $lt: 2010}})
// and
db.movies.find({ $or: [{ rating: {$gt:9}}, {year: {$gte: 2020}})
// or
db.movies.find({ genres: { $in: ["Drama", "Crime"]}})
// ๋ฐฐ์ด ์์
db.movies.find({ genres: { $all: ["Drama", "Crime"]}})
// ๋ฐฐ์ด
db.movies.find({ title: { $regex: /the/i }})
// ์ ๊ทํํ์
db.movies.find({ genres: { $size: 3}})
// ํฌ๊ธฐ
db.movies.find({ director: { $exists: false }})
// ํน์ key ๊ฐ ์กด์ฌ
db.movies.find({ "cast.0": "Keanu Reeves"})
// ๋ฐฐ์ด ๋ด ์์ ๊ฐ
db.movies.find({ "director.alive": true })
// ์ค์ฒฉ๋ ๋ฌธ์ ๋ด๋ถ ๊ฒ์
db.movies.find().skip(10)
// skip
db.movies.find().limit(10).skip(10)
// limit
db.movies.find().sort({rating: 1}).limit(10).skip(10)
db.movies.find().sort({rating: -1}).limit(10).skip(10)
db.movies.find().sort({rating: -1, title: 1}).limit(10).skip(10)
// sort
#19.4 Updating Documents
db.movies.updateOne(
{
_id: ObjectId('66d7b10cc0848d2fccbe1b38')
},{
$set: {'director.name': 'Francing Ford Coppola'},
$currentDate: {updated_at : true}
})
db.movies.findOneAndUpdate(
{
_id: ObjectId('66d7b10cc0848d2fccbe1b38')
},{
$set: {'director.name': 'Francing Ford Coppola'},
$currentDate: {updated_at : true}
}, {returnNewDocument: true, upsert: true}
)
// ์์ ๋น์ทํ์ง๋ง ์ถ๊ฐ์ ์ผ๋ก ๋ค๋ฅธ ์ค์ ๊ฐ๋ฅ
db.movies.updateMany(
{ director: "Christopher Nolan"},
{ $inc: {rating: 0.2}}
)
// ์ฌ๋ฌ ๋ฐ์ดํฐ ๊ฐ ์
๋ฐ์ดํธ
db.movies.updateMany(
{ title: "Inception"},
{ $push: {genres: "Mind Control"}}
)
// ์ถ๊ฐ
db.movies.updateMany(
{ title: "Inception"},
{ $push: {genres: "Mind Control"}}
)
// ์ญ์
db.movies.updateMany(
{ title: "The Dark Knight"},
{ $addToSet: {csst: "Micheal Cane!"}}
)
// ์๋ ๋ฐ์ดํฐ ์ถ๊ฐ
db.movies.updateMany({}, {$rename: {runtime: "duration"}})
// key ๋ณ๊ฒฝ
db.movies.updateOne(
{ title: "Inception" },
{ $set: {
boxOffice: {~~}
}
)
// Embeded Documents ์
๋ฐ์ดํธ
db.movies.updateMany(
{ $expr: { $lt: [{$size: "$genres"}, 3] } },
{ $addToSet: { genres: { $each: ["Other", "Happy"]}}}
)
db.movies.deleteMany({})
// ์ญ์
#19.5 Aggregating Documents
๋ฐ์ดํฐ ์ง๊ณ, group by์ ๋น์ทํจ
db.movies.aggregate(
[
{$count: "total_movies"}
]
)
db.movies.aggregate([
{$group: { _id: null, avgRating: { $avg: "$rating"}}}
])
db.movies.aggregate([{$unwind: "$genres"}])
// ๋ฐฐ์ด ํด์
db.movies.aggregate([
{$unwind: "$genres"},
{$group: {
_id: "$genres",
count: {$sum:1}
}},
{$sort: {count: -1}}
])
db.movies.aggregate([
{$group: {
_id: null,
oldestMovie: {$min: "$year"},
newestMovie: {$max: "$year"}
}}
])
db.movies.aggregate([
{$group: {
_id: "$year",
avgDuration: {$avg: "$duration"},
}},
{$sort: {_id: -1}}
])
db.movies.aggregate([
{$match: {director: {$exists: true}}},
{$group: {
_id: "$director",
movieCount: {$sum: 1},
}},
{$sort: {movieCount: -1}}
])
db.movies.aggregate([
{$unwind: '$cast'},
{$group: {
_id: "$cast",
movieCount: {$sum: 1},
}},
{$sort: {movieCount: -1}}
])
db.movies.aggregate([
{$project: {title:1, director:1, cast:1, _id:0}},
{$limit: 10}
])
Last updated
Was this helpful?