몽고디비 작업을 쉽게 할 수 있도록 도와주는 라이브러리
const connect = () => {
if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true);
}
mongoose.connect('mongodb://ID이름:비밀번호이름@localhost:27017/로그인을하기위한DB이름', {
dbName: 'DB이름',
useNewUrlParser: true,
}).then(() => {
console.log("몽고디비 연결 성공");
}).catch((err) => {
console.error("몽고디비 연결 에러", err);
});
};
mongoose.connect
('mongodb://ID이름:비밀번호이름@localhost:27017/로그인을하기위한DB이름',
{
dbName: 'DB이름',
useNewUrlParser: true
})
const mongoose = require('mongoose');
const connect = () => {
if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true);
}
mongoose.connect('mongodb://ID이름:비밀번호이름@localhost:27017/로그인을하기위한DB이름', {
dbName: 'DB이름',
useNewUrlParser: true,
}).then(() => {
console.log("몽고디비 연결 성공");
}).catch((err) => {
console.error("몽고디비 연결 에러", err);
});
};
mongoose.connection.on('error', (error) => {
console.error('몽고디비 연결 에러', error);
});
mongoose.connection.on('disconnected', () => {
console.error('몽고디비 연결이 끊겼습니다. 연결을 재시도합니다.');
connect();
});
module.exports = connect;
schemas 폴더 안에 작성
// schemas/users.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
name: {
type: String,
required: true,
unique: true,
},
age: {
type: Number,
required: true,
},
married: {
type: Boolean,
required: true,
},
comment: String,
createdAt: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model('User', userSchema);
// schema/comment.js
const commentSchema = new Schema({
commenter: {
type: ObjectId,
required: true,
ref: 'User',
},
comment: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});
기존의 mongoDB에는 join기능이 없는데 mongoose에는 위처럼 ref로 어떠한 schema를 참조하는지 선언 해주고,
router.post('/', async (req, res, next) => {
try {
const comment = await Comment.create({
commenter: req.body.id,
comment: req.body.comment,
});
console.log(comment);
const result = await Comment.populate(comment, { path: 'commenter' });
res.status(201).json(result);
} catch (err) {
console.error(err);
next(err);
}
});
router에서 comment스키마가 호출될 때 populate 를 이용해 JOIN의 기능을 내준다.
populate로 join의 기능을 이용할 시, 중복도 사라지고, 변경이 바로 반영이 된다는 장점이 있지만, js문법이기 때문에 cost가 많이 들게 된다.
따라서 ObjectId로 id를 선언해주고, 직접 관계id에 객체로 데이터를 넣어줄 시, 직접 수정해야 하고, 기입해야한다는 단점이 있지만, cost가 매우 적게 든다.
때문에 서비스 특성에 따라서 알맞은 것을 쓰면 됨