nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
528 stars 118 forks source link

MongoDB Schema prop `unique` only works when `type` is set #858

Closed MickL closed 3 years ago

MickL commented 3 years ago

Bug Report

Current behavior

Setting the prop unique: true has no effect if type is not set.

I am not sure if this is intentional, but I do not see that documented so if it is intentional it should be documented why it is.

Input Code

In the following schema the unique is not working:

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

export type CatDocument = Cat & Document;

@Schema()
export class Cat {
  @Prop({
    required: true,
    unique: true,
  })
  name: string;

  @Prop({required: true})
  age: number;

  @Prop()
  breed: string;
}

export const CatSchema = SchemaFactory.createForClass(Cat);

The following IS working:

@Prop({
    required: true,
    unique: true,
    type: String,
  })
  name: string;

Expected behavior

unique may work without type as required does OR it should be documented.

Environment

Nest version: 7.5.1

kamilmysliwiec commented 3 years ago

Please provide a minimum reproduction repository.

MickL commented 3 years ago

My mistake, works as expected

imSaharukh commented 3 years ago

My mistake, works as expected

where was the mistake bro? I'm facing the issue

achapela commented 3 years ago

My mistake, works as expected

where was the mistake bro? I'm facing the issue

Same here.. What is the mistake????

imSaharukh commented 3 years ago

My mistake, works as expected

where was the mistake bro? I'm facing the issue

Same here.. What is the mistake????

set useCreateIndex: true, autoIndex: true,

like this

  MongooseModule.forRoot(process.env.DATABASE_URL, {
      useCreateIndex: true,
      autoIndex: true,
    })
achapela commented 3 years ago

My mistake, works as expected

where was the mistake bro? I'm facing the issue

Same here.. What is the mistake????

set useCreateIndex: true, autoIndex: true,

like this

  MongooseModule.forRoot(process.env.DATABASE_URL, {
      useCreateIndex: true,
      autoIndex: true,
    })

I was sure I tried that and didn't work. It worked now. Awesome thank you