#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?