Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!