ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ NestJS + MongoDB ์์ ์ ๋ณต – Mongoose ์ฐ๋ ๊ฐ์ด๋
octo54 2025. 5. 27. 11:01๐ NestJS + MongoDB ์์ ์ ๋ณต – Mongoose ์ฐ๋ ๊ฐ์ด๋
NestJS๋ MongoDB๋ฅผ ์ํ ๊ณต์ ๋ชจ๋๋ก **@nestjs/mongoose**๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ด๋ฅผ ํตํด **Mongoose ODM(Object Data Modeling)**์ Nest์ DI(์์กด์ฑ ์ฃผ์
) ์ํคํ
์ฒ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉํ ์ ์์ต๋๋ค.
์ด๋ฒ ๊ธ์ NestJS ๊ณต์ ๋ฌธ์ MongoDB๋ฅผ ๋ฐํ์ผ๋ก,
์ค๋ฌด์์ MongoDB๋ฅผ NestJS๋ก ํจ์จ์ ์ผ๋ก ์ฐ๋ํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํฉ๋๋ค.
๐ก NestJS + Mongoose ์กฐํฉ์ MongoDB๋ฅผ ์ฌ์ฉํ๋ ํ๋ก์ ํธ์ ๋งค์ฐ ์ ํฉํ๋ฉฐ,
๋ณต์กํ Schema ์ค๊ณ์ ์ ์ฐํ ์ฟผ๋ฆฌ ๊ตฌ์ฑ์ด ๊ฐ๋ฅํฉ๋๋ค.
โ 1. ์ค์น
npm install @nestjs/mongoose mongoose
โ 2. MongoDB ์ฐ๊ฒฐ ์ค์
// app.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost/nestdb'), // URI๋ ํ๊ฒฝ๋ณ์๋ก ๊ด๋ฆฌ ๊ถ์ฅ
],
})
export class AppModule {}
โ๏ธ .env ํ์ผ๋ก๋ถํฐ MONGODB_URI ๊ฐ์ ๋ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ์๋ ์ถ์ฒํฉ๋๋ค.
โ 3. ์คํค๋ง ์ ์
NestJS๋ Mongoose์ Schema ํด๋์ค๋ฅผ @Schema() ๋ฐ์ฝ๋ ์ดํฐ๋ก ๊ฐ์๋๋ค.
// user.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
export type UserDocument = User & Document;
@Schema()
export class User {
@Prop({ required: true })
name: string;
@Prop()
email: string;
}
export const UserSchema = SchemaFactory.createForClass(User);
์ด ์ง์ ์ ์คํค๋ง ์ ์์ ์ค์ Nest ์๋น์ค์ ํตํฉ๋๋ ์์ ๊ฐ ๋์ค๋ ํต์ฌ ํํธ์ ๋๋ค.
์ ๋์ผ์ค ๊ด๊ณ ์ฝ์ ์ ์ต์ ์ ๋๋ค.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXX"
data-ad-slot="YYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
โ 4. ์คํค๋ง ๋ชจ๋์ ๋ฑ๋ก
// user.module.ts
import { MongooseModule } from '@nestjs/mongoose';
import { Module } from '@nestjs/common';
import { User, UserSchema } from './user.schema';
import { UserService } from './user.service';
@Module({
imports: [
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
],
providers: [UserService],
})
export class UserModule {}
โ 5. ์๋น์ค์์ Mongoose ๋ชจ๋ธ ์ฃผ์
// user.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from './user.schema';
@Injectable()
export class UserService {
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}
async findAll(): Promise<User[]> {
return this.userModel.find().exec();
}
async create(data: Partial<User>): Promise<User> {
return new this.userModel(data).save();
}
}
โ 6. Controller์์ ์ฌ์ฉ
// user.controller.ts
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
async getAllUsers() {
return this.userService.findAll();
}
@Post()
async createUser(@Body() body: { name: string; email: string }) {
return this.userService.create(body);
}
}
๐ก ์ค๋ฌด ํ – ๋ณต์กํ ๊ด๊ณ ์ค๊ณ
- MongoDB๋ ์ฐธ์กฐํ ๊ด๊ณ(Population) ๋๋ ๋ด์ฅํ(Embedded) ์ค๊ณ๋ฅผ ์ง์ํฉ๋๋ค.
- NestJS + Mongoose์์๋ ์๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฐธ์กฐ ์ค์ ๊ฐ๋ฅ:
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
author: User;
this.postModel.find().populate('author').exec();
๐ง NestJS + MongoDB ์ค์ ์ ๋ต
๊ธฐ๋ฅ NestJS ๊ตฌ์ฑ ์์
Mongo ์ฐ๊ฒฐ | MongooseModule.forRoot() |
์คํค๋ง ์ ์ | @Schema(), @Prop() |
DB ์์ ์ํ | @InjectModel()์ผ๋ก ์ฃผ์ ๋ฐ์ Model<T> ์ฌ์ฉ |
๋ฌธ์ ๊ด๋ฆฌ (Document) | T & Document ํ์ ์ผ๋ก ํ์ ์์ ์ฑ ํ๋ณด |
๊ด๊ณ ์ค์ (Population) | ref: '๋ชจ๋ธ๋ช ' ์ผ๋ก ๊ด๊ณ ์ค์ + populate() ์ฌ์ฉ |
โ ์ฅ์ ์์ฝ
- โ Nest์ DI์ Mongoose ๋ชจ๋ธ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ
- โ ๋น ๋ฅด๊ณ ์ ์ฐํ NoSQL ์ค๊ณ ๊ฐ๋ฅ
- โ Schema, Document, Model์ ๋ช ํํ ๋ถ๋ฆฌ
- โ populate, Index ๋ฑ Mongoose์ ๋ชจ๋ ๊ธฐ๋ฅ ์ง์
NestJS MongoDB,NestJS Mongoose,NestJS NoSQL ์ฐ๋,NestJS Mongo ์คํค๋ง,NestJS MongooseModel ์ฌ์ฉ๋ฒ,NestJS DB ์ฐ๋ ๊ฐ์ด๋,NestJS Mongo ์ค๋ฌด,NestJS ODM,NestJS populate ๊ด๊ณ ์ค์ ,NestJS Document ๋ชจ๋ธ
'framework > NestJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- nodejs
- App Router
- seo ์ต์ ํ 10๊ฐ
- ๋ฅ๋ฌ๋
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- Ktor
- NestJS
- Prisma
- JAX
- nextJS
- ์น๊ฐ๋ฐ
- Webpack
- ํ๋ก ํธ์๋
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก ํธ์๋๋ฉด์
- rag
- Python
- SEO ์ต์ ํ
- fastapi
- gatsbyjs
- SEO์ต์ ํ
- kotlin
- AI์ฑ๋ด
- llm
- PostgreSQL
- Docker
- ๋ฐฑ์๋๊ฐ๋ฐ
- CI/CD
- REACT
- Next.js
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |