mongoose는 몽고디비와 node.js를 연결 시켜주는 역
db를 mongoose와 연결시켜 video model을 인식시킴
models/Video.js => 데이터가 어떻게 생겼는지 정의
새롭게 생성하는 object 내에 id를 랜덤으로 부여해줌
Video.findByIdAndUpdate(id, update할 것)
Video.findById(id) // id를 찾음
Video.exists() // 결과 true or false ()안엔 filter
Video.findByIdAndDelete & findOneAndRemove ==> 특별한 이유 없는 이상 대부분 delete
Model
Video = model
video = DB 안에 있는 object
📌 static function 만들수 있다.
const videoSchema = new mongoose.Schema({
title: { type: String, required: true, trim: true, maxLength: 80 },
description: { type: String, required: true, trim: true, minLength: 20 },
createdAt: { type: Date, required: true, default: Date.now },
hashtags: [{ type: String, trim: true }],
meta: {
views: { type: Number, default: 0, requird: true },
rating: { type: Number, default: 0, requird: true },
},
});
videoSchema.static("formatHashtags", function (hashtags) {
return hashtags
.split(",")
.map((word) => (word.startsWith("#") ? word : `#${word}`));
});
const Video = mongoose.model("Video", videoSchema);