์ธํ๋ฐ์์ Node.js React.js๊ฐ์๋ฅผ ๋ฃ๊ณ ์๋๋ฐ ๊ทธ ๊ฐ์์์ MongoDB๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ธ์ ์ผ๋ก ๊ณต๋ถํ ๊ฒธ ํฌ์คํ
์ ์งํํ๊ฒ ๋์๋ค. ์ด์ ์ ๋ด๊ฐ ์ฌ์ฉํ๋ mysql, oracle๊ณผ ๋ค๋ฅธ nosql
์์คํ
์ด๋ค. ๋ฐ๋ผ์ ๋ฌธ๋ฒ๋ ๋ค๋ฅด๊ณ ๊ฐ๋
๋ ๋ค๋ฅด๊ธฐ์ ์ต์ํ์ง ์๋ค๋ ๋จ์ ์ด ์์ง๋ง, ์ต์ํด์ง๋ฉด ์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ์ง ์๊ณ ํจ์ฌ ๋น ๋ฅด๊ฒ ์งํํ ์ ์์ ๊ฒ ๊ฐ๋ค.
MongoDB๋?
๋ชฝ๊ณ DB๋ ํฌ๋ก์ค ํ๋ซํผ ๋ํ๋จผํธ ์งํฅ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ
์ด๋ค. NoSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ๋ถ๋ฅ๋๋ ๋ชฝ๊ณ DB๋ JSON๊ณผ ๊ฐ์ ๋์ ์คํค๋งํ ๋ํ๋จผํธ๋ค์ ์ ํธํจ์ ๋ฐ๋ผ ์ ํต์ ์ธ ํ
์ด๋ธ ๊ธฐ๋ฐ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ตฌ์กฐ์
์ฌ์ฉ์ ์ผ๊ฐ๋ค.
์ฐธ์กฐ : https://ko.wikipedia.org/wiki/%EB%AA%BD%EA%B3%A0DB
MongoDB method~
save
save๋ MongDB์์ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๋ ๋ฉ์๋์ด๋ค. save๋ง๊ณ ๋ insert ๋ฑ์ด ์๋ค.
์ฌ์ฉ์์
1
2
3
4
5
6
7
8
9
10
11
|
router.post('/', (req, res) => {
//๊ฐ์ ธ์จ ์ ๋ณด๋ค์ db์ ๋ฃ์ด์ค๋ค.
//๊ฐ์ ธ์จ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
const product = new Product(req.body)
//๊ทธ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ DB์ ์ ์ฅํ๋ค.
product.save((err) => {
if (err) return res.status(400).json({success: false, err})
return res.status(200).json({success: true})
})
})
|
์ ์์ ์ฒ๋ผ ์คํค๋ง์ body๋ฅผ ๋ด์ save๋ฅผ ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
find
find๋ ๋ง ๊ทธ๋๋ก ์กฐํ ๋ฉ์๋์ด๋ค. find({}) ๋๋ find()๋ฅผ ํ๋ฉด ์ปฌ๋ ์
๋ด์ ๋ชจ๋ ๋คํ๋จผํธ๋ค์ ์ ํํ์ฌ ๊ฐ์ ธ์จ๋ค.
์ฌ์ฉ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//๊ธฐ๋ณธ์์
Product.find({})
//๋ชจ๋ ์ผ์นํด์ผํ๋ ๊ฒฝ์ฐ๋ ์ผํ๋ก๊ตฌ๋ถํ์ฌ ์์ฑํ๋ค.
Product.find({name: 'test', job: '๊ฐ๋ฐ์'})
//ํ๊ฐ์ง๋ง ์ผ์นํด๋๋๋ ๊ฒฝ์ฐ๋ $or๋ฅผ ์ฌ์ฉํ๋ค.
Product.find({$or: [{name: 'test'}, {job: '๊ฐ๋ฐ์'}]})
//์ค์ ์์
router.post('/products', (req, res) => {
//product collection์ ๋ค์ด์๋ ๋ชจ๋ ์ํ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
//์กฐ๊ฑด์ Product.find({price:..}) ์ด๋ฐ์์ผ๋ก ๋ค์ด๊ฐ๋ค.
Product.find()
.populate('writer')
.exec((err, productInfo) => {
if (err) return res.status(400).json({success: false, err})
return res.status(200).json({success: true, productInfo})
})
})
|
์์ ๋ฅผ ๋ณด๋ฉด ๋์ถฉ ํ์
ํ๋ฏ์ด find๋ฉ์๋์์ jsonํํ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ ๊ฒ์ ์ ์ ์๋ค.
์ซ์๋น๊ต ์ต์
ํฐ ๊ฒ์ $gt, ์์๊ฒ์ $lt, ํฌ๊ฑฐ๋ ๊ฐ์๊ฒ์ $gte, ์๊ฑฐ๋ ๊ฐ์ ๊ฒ์ $lte์ด๋ค.
์ฌ์ฉ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
router.post('/products', (req, res) => {
//product collection์ ๋ค์ด์๋ ๋ชจ๋ ์ํ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
//์กฐ๊ฑด์ Product.find({price:..}) ์ด๋ฐ์์ผ๋ก ๋ค์ด๊ฐ๋ค.
let limit = req.body.limit ? parseInt(req.body.limit) : 20
let skip = req.body.skip ? parseInt(req.body.limit) : 0
//์กฐ๊ฑด์์ ๋ง๋ค์ด์ค์ผํ๊ธฐ์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ค๋ค.
let findArgs = {}
//์ด๋ ๊ฒ ๋๋ฉด ๋ฐฐ์ด์์ ๋ฐฐ์ด ์ฆ, price๋ continents๊ฐ key๊ฐ ๋๋ค.
for (let key in req.body.filters) {
//๋ฐฐ์ด์ ๊ฐ์ด ์กด์ฌํ๋ค๋ฉด ์๋๋ฅผ ์ํํ๋ค.
if (req.body.filters[key].length > 0) {
//์กฐ๊ฑด์์ ๋ฃ์ด์ฃผ๋ ๋ถ๋ถ์ด๋ค.
if (key === 'price') {
findArgs[key] = {
//๊ฐ์ [0.199] ์ด๋ฐ์์ผ๋ก ๋ค์ด์จ๋ค.
//greater than equal -> ์ด๊ฒ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ
$gte: req.body.filters[key][0],
//less than equal => ์ด๊ฒ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๊ณ
$lte: req.body.filters[key][1]
}
} else {
//๊ทธ๊ฒ์๋๋ผ ๊ทธ๋ฅ ๊ฐ์ผ๋ก find๋๋ ค๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ ๊ฐ์๋ฃ์ด๋ฒ๋ฆผ
findArgs[key] = req.body.filters[key];
}
}
}
console.log(findArgs)
Product.find(findArgs)
.populate('writer')
.skip(skip)
.limit(limit)
.exec((err, productInfo) => {
if (err) return res.status(400).json({success: false, err})
return res.status(200).json({success: true, productInfo, postSize: productInfo.length})
})
})
|
ํฌ์ฌ(projection)
ํฌ์ฌ(projection)์ด๋ find์ findOne ๋ฉ์๋์ ๋ ๋ฒ์งธ ์ธ์๋ก ๋ฃ์ด์ฃผ๋ ๊ฒ์ ๋งํ๋ค. select ์ ์ ์ปฌ๋ผ์ ์ฐ๋ ๊ฒ๊ณผ ๊ฐ์ ํจ๊ณผ์ด๋ค. projection ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ์ฉ๋๋ ์ ์ฝํ ์ ์๊ณ ,
๋ณด์์๋ ์ข๋ค.
1
|
Product.find({name: 'Slime'}, {name: true, hp: true, _id: false})
|
update
์
๋ฐ์ดํธ๋ ๋ณดํต update๋ FindAndModify๋ฅผ ์ฌ์ฉํ๋ค.
####update
1
|
Product.update({name : 'test'}, { $set : { hp : 30}})
|
์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ์ํ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฐ๋์ $set์ ์ฌ์ฉํด์ผ ํด๋น ํ๋๋ง ๋ณ๊ฒฝ๋๋ค.
๋ง์ฝ $set์ ๋ฃ์ง ์์ผ๋ฉด, ๊ฐ์ฒด๊ฐ {hp : 30}์ผ๋ก ๋ณ๊ฒฝ๋๋ค.
$inc๋ฅผ ์ฌ์ฉํ๋ฉด ์ซ์๋ฅผ ์ฌ๋ฆฌ๊ฑฐ๋ ๋ด๋ฆด ์ ์๋ค. ์์๋ฅผ ๋ฃ์ผ๋ฉด ๋ด๋ ค๊ฐ๊ณ ์์๋ฅผ ๋ฃ์ผ๋ฉด ์ฌ๋ผ๊ฐ๋ค.
1
|
Product.update({name : 'test'}, { $inc : { hp : -5 }})
|
####FindAndModify
update ๋ฉ์๋์ ๋ฌ๋ฆฌ upsert์ remove๊น์ง ์ํํ ์ ์๋ค.
- upsert -> update + insert์ด๋ค.
์์ ํ ๋์์ด ์๋ ๊ฒฝ์ฐ insert ๋์์ ์ํํ ์ ์๊ฒํ๋ค.
- upsert ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ๋ฉ์๋์ ์ต์
์ผ๋ก {upsert : true}๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
1
|
Product.findAndModify({query : {name : 'test'}, update : {$set : {att : 150}, new : true}})
|
delete
MongoDB๋ Rollback์ ์ง์ํ์ง ์๋๋ค.
1
2
3
4
5
|
//์ ์ฒด๊ฐ ์ง์์ง
Product.remove({})
//ํด๋นํ๋ ์ด๋ฆ์ ๋ํ๋จผํธ๋ง ์ง์์ง
Product.remove({name : 'zedd'})
|