mongoose(2)
-
Mongoose 스키마 설정
const mongoose = require("mongoose"); const Schema = mongoose.Schema; const placeSchema = new Schema({ title: {type: String, required : true}, description: {type: String, required : true}, image : {type: String, required : true}, address: {type: String, required : true}, location : { lat : {type: Number, required : true}, lng : {type: Number, required : true} }, creator : {type: String, required..
2024.01.14 -
백엔드 MongoDB 데이터 베이스에 연결하기 (Mongoose 사용)
Mongoose를 사용하지 않으면, 새로운 것을 추가할 때마다 MongoClient를 사용하고 / connect 메서드로 연결을 설정해야 했음 심지어 close()로 연결을 종료하기까지 해야 함 Mongoose에는 메서드가 딱 하나만 필요함 mongoose.connect("\ url") 위처럼 하면 새롭게 생성할 때마다 새 연결을 여닫을 필요가 없음 이렇게 Mongoose는 데이터베이스와 백엔드가 항상 효율적으로 연결되도록 함 다만 Mongoose의 경우 사용하기가 조금 어렵다는 단점이 있는데, 이는 백엔드와 데이터베이스의 연결을 관리하는 것 외에도 프로미스를 반환함 (then을 추가해야 함) 즉, mongoose를 사용하지 않으면, 새로운 것을 생성할 때마다 mongoClient를 사용해야 함 아래 코..
2024.01.10