Nest Mongoose _id 인덱스 속성 명칭 변경 방법

2022. 4. 19. 19:24Nest.js

사용방법

mongoose의  Types를 import 하여

변경하려는 속성에 타입을 지정해 준다.

 

import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';

const options: SchemaOptions = {
  timestamps: true,
  collection: 'users',
};

@Schema(options)
export class Test extends Document {
  @Prop({
    required: true,
    unique: true,
  })
  id: Types.ObjectId;
}

 

사용

1. 속성 명 변경 전 

import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose';
import { Document } from 'mongoose';

const options: SchemaOptions = {
  timestamps: true,
  collection: 'users',
};

@Schema(options)
export class Test extends Document {
  @Prop({
    required: true,
    unique: true,
  })
  id: string;

  @Prop({
    required: true,
  })
  password: string;

  @Prop({
    required: true,
  })
  salt: string;

  @Prop({
    required: true,
    unique: true,
  })
  nickName: string;
}

export const TestSchema = SchemaFactory.createForClass(Test);

2. 속성 명 변경 후

import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose';
/// 변경
import { Document, Types } from 'mongoose';

const options: SchemaOptions = {
  timestamps: true,
  collection: 'users',
};

@Schema(options)
export class Test extends Document {
  @Prop({
    required: true,
    unique: true,
  })
  /// 변경
  id: Types.ObjectId;

  @Prop({
    required: true,
  })
  password: string;

  @Prop({
    required: true,
  })
  salt: string;

  @Prop({
    required: true,
    unique: true,
  })
  nickName: string;
}

export const TestSchema = SchemaFactory.createForClass(Test);
728x90
반응형