this is my schema for Mongodb:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { MinioBucket } from '../../../common/enums';
import { Document } from 'mongoose';
@Schema({ versionKey: false, timestamps: true })
export class Column extends Document {
@Prop({ type: String, required: true })
tableCol: string;
@Prop({ type: String, required: true })
sheetCol: string;
}
const ColumnSchema = SchemaFactory.createForClass(Column);
@Schema({ versionKey: false, timestamps: true })
export class Col extends Document {
@Prop({ type: String, required: true, unique: true })
public readonly provider!: string;
@Prop({ type: [ColumnSchema] })
cols: Column[];
}
const ColSchema = SchemaFactory.createForClass(Col);
ColSchema.index({ provider: 1 });
export { ColSchema };
in database I saved: { provider: "abc", columns: [ {"tableCol": "name" "sheetCol": "Name"} ] }
Now when I want to add another provider, but with the same columns name I recieve the error: ERROR [ExceptionsHandler] E11000 duplicate key error collection: xxx.columns index: columns.tableCol_1 dup key: { columns.tableCol: "name" } MongoServerError: E11000 duplicate key error collection: xxx.columns index: columns.tableCol_1 dup key: { columns.tableCol: "name" }
Data I want to add: { provider: "cde", columns: [ {"tableCol": "name" "sheetCol": "Name"} ] }
I tried to put {unique: false}, and I found on internet about {spars: true}, but it didn't help
And I cannot drop the Index, I need to have both data in database