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

193
Visualizações
MongoDb duplicate key erro

I am getting this error while trying to insert data in the MongoDB collection.

MongoError: E11000 duplicate key error collection: kaa.logs_96775772132708234336 index: id dup key: { _id: ObjectId('6001bdc2429b9313d755e106') }

Here is my code :

MongoClient.connect(url, function(err, db) {
        if (err) throw err;

        var dbo = db.db("kaa");

        dbo.collection("logs_96775772132708234336").insertOne(document, function(err, res) {
            if (err) throw err;
            console.log("1 document inserted");
            console.log(res.insertedId);
            db.close();
        });
    });

When I am searching for the same object id, I am getting data. It seems NodejS Mongo is creating a duplicate object ID.

Any help is much appreciated.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I do suspect that after insertOn your entity document is reciveng _id filed, which in result casues that you are insrting document with that _id again.

Check your document after inserting it

console.log(document._id);

As in mongo docs

_id Field

If the document does not specify an _id field, then mongod will add the _id field and assign a unique ObjectId for the document before inserting. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.

If the document contains an _id field, the _id value must be unique within the collection to avoid duplicate key error.

over 4 years ago · Santiago Trujillo Relatório

0

I had a error like that before and the solution was to delete the collection itself and try again .

over 4 years ago · Santiago Trujillo Relatório
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