Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
How can I solve a "MongoError: E11000 duplicate key error collection" error in Mongo? There is no duplication

I have tried every answer is StackOverflow. But none of this answered not working for me or my question, I have created a new Mongo collection for a new project, and I am adding data to this collection using the map() function.

Mongo collection:-

@Schema({ timestamps: true })
export class TagServiceEntity {
  @Prop({
    type: String,
    default: function genUUID() {
      return uuidv4();
    },
  })
  _id: string;
  @Prop({
    type: String,
    required: true,
    trim: true,
    index: true,
  })
  name: string;

  @Prop({ type: String, slug: 'name', unique: true })
  slug: string;
  @Prop()
  tenantId: string;
}

export const TagServiceSchema = SchemaFactory.createForClass(TagServiceEntity);

Map function:-

async setTags(tags: Array<string>, tenantId: string): Promise<Array<string>> {
    if (tags.length > 0 && tags[0] !== '') {
      return await Promise.all(
        tags.map(async (tag: any) => {
          const isTagExist = await this.tagsService.checkTagExist(tag);
          if (isTagExist) {
            return this.tagsService.findByTagName(tag);
          }
          const newTagData = {
            name: tag.toString(),
            tenantId: 'test-tenantId-01',
          };
          const newTag = await this.tagsService.create(newTagData);
          return newTag._id;
        }),
      );
    }
    return [];
  }

DAO Function: -

async create(createTagServiceDto: any) {
    console.log(createTagServiceDto);
    try {
      const createTags = new this.tagsModel(createTagServiceDto);
      const tags = await createTags.save();
      if (tags) {
        return tags;
      }
    } catch (error) {
      console.log(error);
      throw error;
    }
  }

This is my error :- enter image description here

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda