๋ฐ์ํ
๐คทโโ๏ธMongoose๋ ๋ฌด์์ผ๊น?
- Mongoose ๋ชจ๋์ MongoDB ๋ผ๋ NoSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ Node.js๋ก ์ฌ์ฉํ ์ ์๋๋ก ํ๋ ํ์ฅ ๋ชจ๋ ์ค ํ๋ ์ ๋๋ค.
- Mongoose๋ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํด ์คํค๋ง [ Schema ]๋ฅผ ๋ง๋ค๊ณ , ๊ทธ ์คํค๋ง๋ก ๋ชจ๋ธ์ ๋ง๋ค์ด ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌ ํฉ๋๋ค.
- ์คํค๋ง์ ๋ชจ๋ธ์ ํตํ์ฌ data๋ฅผ ๋ถ๋ฌ์จ ํ ๊ฐ์ฒดํ ์์ผ ๋น ๋ฅด๊ฒ ์์ ํจ์ผ๋ก์จ ๋ฐ์ดํฐ์ ์ ๊ทผ ๊ฐ๋ฅํ๊ฒ ๋ง๋ค์ด์ค๋๋ค.
- ๋ชจ๋ธ๋ง ๋ ๋ฌธ์ [ Document ]๊ฐ ๋ชจ์ฌ์๋ Collection์ ๊ด๋ฆฌํ๋ ๊ฒ์ ์์ํ๊ฒ ๋ง๋ค์ด ์ค๋๋ค.
๐ ์ฃผ์ ๋ฉ์๋ ์ฌ์ฉํด๋ณด๊ธฐ
๐ป๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ฐ๊ฒฐํ๊ธฐ
const mongoose = require('mongoose')
mongoose.connect("mongodb://127.0.0.1:27017/UserAPI")
.then(() => {
console.log("Connected to MongoDB => UserAPI");
})
.catch((err) => {
console.log(err);
});
const mongoose = require('mongoose`)
๋ฅผ ํตํด mongoose ๋ชจ๋์ ๋ถ๋ฌ์ต๋๋ค.- ๊ทธ ํ
mongoose.connetc("๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ฃผ์")
๋ฅผ ํตํด MongoDB์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
๐ก ์ด๋ Mongoose query๋ then() ํจ์๋ฅผ ๊ฐ์ง thenable ์ด๊ธฐ ๋๋ฌธ์ ํ๋ก๋ฏธ์ค๋ฅผ ์ด์ฉํ๋ฏ์ด ์ด์ฉํ ์ ์์ต๋๋ค.
- then ์ ํตํด ์ฐ๊ฒฐ์ ์ฑ๊ณต ํ์ ๋์ ์คํจ ํ์๋๋ฅผ ๊ตฌ๋ถํด์ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ ์ ์์ต๋๋ค.
๐ซ ๋ชจ๋ธ ์ ์ํ๊ธฐ [ Schema ๋ง๋ค๊ธฐ ]
const mongoose = require('mongoose')
const {Schema} = require('mongoose')
const UserSchema = new Schema({
user_id: {
required: true,
unique: true,
type: String,
},
password: {
required: true,
type: String,
},
salt: {
required: true,
type: String,
},
email: {
required: true,
type: String,
unique: true,
}
})
mongoose.model('users', UserSchema)
- mongoose๋ฅผ ๋ถ๋ฌ์จ ํ, mongoose ์์ Schema๋ฅผ ๊ฐ์ ธ์จ๋ค.
- ๊ทธ ํ
new Schema
๋ฅผ ํตํด Schema๋ฅผ ๊ตฌ์ฑํด ์ค๋ค. - ๊ทธ ํ ์์ฑํด์ค Schema๋ฅผ ์ฌ์ฉํด์
mongoose.model('๋ชจ๋ธ๋ช ', ์ฌ์ฉํ ์คํค๋ง)
๋ฅผ ํตํด ๋ชจ๋ธ์ ์์ฑํด์ค๋๋ค.
๐ ๊ฒ์ํ๊ธฐ!
์กฐ๊ฑด์ ๋ง๋ ๋ชจ๋ ๊ฐ์ ์ฐพ๊ธฐ
userModel.find({user_id: 'admin'}).then((docs) => {
console.log(docs)
})
๋๋
userModel.find({user_id: 'admin'}, (err,docs) => { console.log(docs) })
> #### ์กฐ๊ฑด์ ๋ง๋ ๊ฐ ํ๋๋ง ์ฐพ๊ธฐ
```js
userModel.findOne({user_id: 'admin'}).then((docs) => {
console.log(docs)
})
id ๊ฐ์ผ๋ก ๊ฒ์ํ๊ธฐ
userModel.findById("621b574ff0bfe6d32b330505").then((docs) => {
console.log(docs)
})
๐พ ๋ํ๋จผํธ ์ถ๊ฐ ( ๊ฐ ์ ์ฅ )
๋ชจ๋ธ ์ธ์คํด์ค ์์ฑ ํ ์ถ๊ฐํ๊ธฐ
const userInfo = {
user_id: req.body.user_id,
salt: salt,
password: hash.cryptoPassword(req.body.password, salt),
email: req.body.email,
}
const newUser = await new userModel(userInfo)
newUser.save((err) => {
if (err) {
console.log("POST : ์ ์ ์์ฑ ์คํจ")
return res.status(500).json({
message: `create failed: ${err}`
})
}
console.log(newUser)
res.status(200).json({
message: "create succeed"
})
})
๋ชจ๋ธ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์๊ณ ๋ชจ๋ธ์ ์ด์ฉํ์ฌ ์ถ๊ฐํ๊ธฐ
const userInfo = {
user_id: req.body.user_id,
salt: salt,
password: hash.cryptoPassword(req.body.password, salt),
email: req.body.email,
}
userModel.create(userInfo, (err) => {
if (err) {
console.log("POST : ์ ์ ์์ฑ ์คํจ")
return res.status(500).json({
message: `create failed: ${err}`
})
}
console.log(userInfo)
res.status(200).json({
message: "create succeed"
})
})
๐ ๋ํ๋จผํธ ์ ๊ฑฐ ( ์ญ์ )
const post = await postModel.findOne({title: req.params.title}) if (!post) { console.log("์ญ์ ํ ๊ฒ์๊ธ์ด ์์ต๋๋ค. ") return res.status(500).json({message: "None Data"}) } postModel.deleteOne(post, (err) => { if (err) { return console.log(err); } console.log("์ญ์ ์ฑ๊ณต") })
---
> #### ๐ ๋ํ๋จผํธ ์์ ( Update)
>```js
postModel.updateOne(post, updatePost, (err) => {
if (err) {
console.log(`ํฌ์คํธ ์์ ์คํจ => ${err}`)
return res.status(500).json({message: "Update Failed"})
}
console.log("ํฌ์คํธ ์์ ์ฑ๊ณต")
res.status(200).json({
message: "Update Success",
data: {updatePost}
})
}
๐งต Mongoose Populate [ INNERJOIN ]
Schema ์ค์
const postSchema = new Schema({
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
author: {
type: Schema.Types.ObjectId,
ref: "users",
index: true,
required: true,
},
time: {
type: Date,
default:getDate()
}
,
})
์ ์ฝ๋์ author
๋ถ๋ถ์ type
์ด Schema.Types.ObjectId
์ธ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
์ด๊ฑด users
๋ชจ๋ธ์ id
๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ํ์
์ค์ ์ด๋ฉฐ, ์ด๋ฅผ ์ํด ref
์ ์ฐ๊ฒฐํ ๋ชจ๋ธ์ ์ด๋ฆ์ ์์ฑํด์ค๋๋ค.
populate ์ ๋ฐ์ดํฐ
{
_id: "621b827c92b350447277a06e",
title: "์์ ํ
์คํธ",
body: "์ฑ๊ณตํด๋",
author: "621b574ff0bfe6d32b330505",
time: "2022-02-28T20:14:57.865Z",
__v: 0
},
populate ํ๊ธฐ ์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํด๋ณด๋ฉด author
์์ ObjectId
ํ์
์ผ๋ก ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์ด ๋ฐ์ดํฐ๋ฅผ ์ด์ ์ฌ์ฉํ ์ ์๋๋ก ํผ์น๊ธฐ ์ํด populate๋ฅผ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค.
populate ํ๋ ๋ฒ
try {
const user = await userModel.find({user_id: req.params.author})
postModel.find({author: user}).populate('author').then((post) => {
res.status(200).json(post)
})
} catch (e) {
console.log(`๊ฒ์ ์คํจ => ${e}`)
res.status(500).json({message: "search Failed"})
}
populate ์ฌ์ฉ ํ ๋ฐ์ดํฐ ๋ชจ์ต
{
_id: "621b827c92b350447277a06e",
title: "์์ ํ
์คํธ",
body: "์ฑ๊ณตํด๋",
author: {
_id: "621b574ff0bfe6d32b330505",
user_id: "admin",
password: "30b55922e2cd2752a859b4e2294b10cd47192c5c8f9174b39c4aaf28837d16de",
salt: "b5676ff2b24934a33005986e88ce838fdba6197cdef40dfbf23fa40ad36ddafc",
email: "test@example.ocm",
__v: 0
},
time: "2022-02-28T20:14:57.865Z",
__v: 0
}
๋ฐ์ํ
'Study > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ Node.js ] crypto๋ฅผ ์ฌ์ฉํ์ฌ ๋น๋ฐ๋ฒํธ ์ํธํ ํ๊ธฐ (0) | 2022.02.24 |
---|---|
[ Node.js ] readFile, readFileSync ํจ์ ์์๋ณด๊ธฐ (0) | 2022.02.20 |
[ JS ] Event ์ ๋ฆฌ (0) | 2022.02.08 |
[ JS ] DOM์ ๋ํ ์ด ์ ๋ฆฌ! (0) | 2022.02.08 |
[ JS ] JS์ TDZ๋ ๋ฌด์์ผ๊น? (0) | 2022.02.08 |