1. CRUD란 ?
- create
- read
- update
- delete
2. Cheat Sheet
Schema 세팅
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
name: {
type: String,
required: true
},
brand: {
type: String,
required: false
},
order: {
type: Schema.Types.ObjectId,
ref: "Order"
}
});
// This creates our model from the above schema, using mongoose's model method
var Product = mongoose.model("Product", ProductSchema);
// Export the Article model
module.exports = Product;
연결하기
var mongoose = require("mongoose");
var db = require("./models");
mongoose.connect("mongo_URI", { useNewUrlParser: true });
Create
var product = { name: "Soda", brand: "demoBrand" };
db.Product.create(product)
.then(function(dbProduct) {
console.log(dbProduct);
})
.catch(function(err) {
console.log(err);
});
Read
db.Product.find({}) //모두찾기
db.Product.findOne({ _id: <id> }) //id로 찾기
db.Product.findById(id); // 스키마에서 id로 찾기
Update
db.Product.updateOne({ name: 'Soda' }, { brand: 'newBrand' });
db.Product.updateMany({ brand: 'demoBrand' }, { quantity: 500 }); //많은 문서 업데이트
Delete
db.Product.deleteOne({ name: 'Soda' });
db.Product.deleteMany({ quantity: { $gte: 100 } });
- 서버 다까먹겠다 ! 복습 달려 !
'더이상 하지 않는 Backend - NodeJS > Node-Express 개론(완)' 카테고리의 다른 글
[O'REILLY] Node & Express - 1장 : EXPRESS에 대하여 (0) | 2023.03.06 |
---|---|
Express - 5장 : Authentication - 1 (Session-based Auth) (0) | 2023.03.03 |
Express - 3장 : Mongoose 더 살펴보기 (0) | 2023.02.27 |
Express - 2장 : MongoDB / Mongoose 세팅하기 (0) | 2023.02.27 |
Express - 1장 : 설치부터 요청처리와 REST API 개념정리 (0) | 2023.02.26 |
댓글