반응형
mongoose 설치
yarn add mongoose@5.9.7
example.js
import mongoose from 'mongoose';
const mongooseOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
};
mongoose.connect('mongodb://mongodb.example.com:10000/test', mongooseOptions);
const UserSchema = new mongoose.Schema(
{
name: String,
email: String,
password: String,
},
{ versionKey: false }, // 데이터 저장시 자동으로 "__v" 필트 생성되는 것 방제
);
const User = mongoose.model('User', UserSchema);
User.create({
name: 'john',
email: 'john@example.com',
password: 'john-1234'
}, (err, user) => {
console.log(err, user);
});
생성
User.create({
name: 'john',
email: 'john@example.com',
password: 'john-1234'
}, (err, user) => {
console.log(err, user);
});
User.create({
name: 'tom',
email: 'tom@example.com',
password: 'tom-1234'
}, (err, user) => {
console.log(err, user);
});
조회
User.find({
name: 'john'
}, (err, users) => {
console.log(err, users);
});
수정
// 단일 데이터 수정
User.update({
name: 'john'
}, {
$set: {
password: 'john-4567'
}
}, (err, users) => {
console.log(err, users);
});
// 복수 데이터 수정
User.updateMany({
name: 'john'
}, {
$set: {
password: 'john-4567'
}
}, (err, users) => {
console.log(err, users);
});
삭제
User.remove({
name: 'john'
}, (err, users) => {
console.log(err, users);
});
참고
반응형
'Development > MongoDB' 카테고리의 다른 글
[MongoDB] Spring 연동 (0) | 2020.12.29 |
---|---|
[MongoDB] 데이터 백업/복구 (0) | 2020.12.29 |
[MongoDB] Replica Set 구성하기 (0) | 2020.12.29 |
[MongoDB] Query (0) | 2020.12.29 |
[MongoDB] 설치하기 (0) | 2020.12.29 |