Web/Node.js
[Node.js] mongoDB Schema 생성
choice91
2021. 9. 18. 00:00
728x90
반응형
📌 Schema를 생성하는 법
문서에 가보면 방법을 친절히 설명해주고 있다.
import mongoose from "mongoose";
const videoSchema = new mongoose.Schema({
title: String, // String is shorthand for { type: String }
description: String,
createdAt: Date,
hashtags: [{ type: String }],
meta: {
views: Number,
rating: Number,
},
});
- title필드의 타입은 문자열
- description의 타입도 문자열
- createAt의 타입은 날짜
- hashtags는 문자열의 배열이다.
- meta는 객체가 들어간다. views와 rating의 타입은 숫자
String이라고 적어줘도되고 { type: String }이라고 적어줘도 된다.
📌 Model 생성
모델은 모델의 이름과 데이터의 형식인 schema로 구성하면 된다.
const Video = mongoose.model("Video", videoSchema);
728x90
반응형