이주동안의 백엔드 수업이 끝났지만 단기간에 많은양을 배웠고, 정리는 필수라고 생각이 들어 포스팅 하게 되었습니다. 혹시라도 잘못된점이 있다면 댓글 부탁드립니다.
스키마를 정의하지 않고 데이터를 사용할 수 있는 것은 NoSQL의 장점이지만, 데이터 형식을 미리 정의해야 코드 작성과 프로젝트 관리에 유용함 Mongoose는 Code-Level에서 스키마를 정의하고 관리할 수 있게 해줌
MongoDB는 기본적으로 Join을 제공하지 않음 Join과 유사한 기능을 사용하기 위해선 aggregate라는 복잡한 쿼리를 해야 하지만, Mongoose는 populate를 사용하여 간단하게 구현할 수 있음
const{ Schema } = require('mongoose');
constPostSchema= newSchema({title: String, content: String,},
{timestamps: true,});
module.exports=PostSchema;
constmongoose = require('mongoose');
const PostSchema= require('./schemas/board');
exports.Post= mongoose.model('Post', PostSchema)
constmongoose = require('mongoose');
const{ Post }require('./models');
mongoose.connect('mongodb://localhost:27017/myapp');
CREATE: create
READ: find, findById, findOne
UPDATE: updateOne, updateMany,findByIdAndUpdate, findOneAndUpdate
DELETE: deleteOne, deleteMany,findByIdAndDelete, findOneAndDelete
const created = awaitPost.create({title: 'first title',content: 'second title',});
const multpleCreated= awaitPost.create([item1, item2]);
const listPost= awaitPost.find(query);
Person.find({name: 'kyubum',age: {$lt: 20,$gte: 10,},
languages: {$in: ['ko', 'en'],},$or:[{ status: 'ACTIVE'}, { isFresh: true},],});
Person.find({ name:['elice','bob']});
[https://docs.mongodb.com/manual/reference/operator/query/](몽고디비 쿼리)
constupdateResult= awaitPost.updateOne(query, {...});
constdeleteResult= awaitPost.deleteOne(query);
constPost = newSchema({...user: { type: Schema.Types.ObjectId, ref: 'User' },
comments: [{type: Schema.Types.ObjectId,ref: 'Comment',}],});
const post = awaitPost.find().populate(['user', 'comments']);
Document 안에 Document를 담지 않고, ObjectID를 가지고 reference 하여 사용할 수 있는 방법을 제공함
Document에는 reference 되는 ObjectID를 담고, 사용할 때 populate 하여 하위 Document처럼 사용할 수 있게 해줌