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

152
Views
MongoDB showing 'Duplicate Key' error on updating JS Object using react, even though the entity is unique

The application contains various programs which have courses mapped to it. I'm trying to make modifications to the courses array using a drag and drop (dragging courses from 'Available Courses' to 'Mapped Courses' of a program).

'createProgram' function is called when an update request is made. It takes the initial values of the existingProgram, maps the new courses which come from a drap and drop component. Then passes the new object to the update route.

const createProgram = () => {
  
  newProgram = {
        _id: existingProgram._id,
        info: {
          name: existingProgram.name, // {required: true, unique: true, trim: true}
          overview: existingProgram.overview, // {default: null, trim: true}
          thumbnail: existingProgram.thumbnail, // {default: null, trim: true}
          subtitle: existingProgram.name
            .toLowerCase()
            .replace(/ /g, '-')
            .replace(/[^\w-]+/g, '')
            .replace(/-+/g, '-'), // {required: true, unique: true, trim: true}
    },
    // populating 'courses' array with pre-existing values then mapping the new courses
    // coming from 'Mapped Courses' column of react Drag & Drop
    courses: [
          ...existingCourses,
          columns['Mapped Courses'].items.map((course, index) => ({
            id: course.id,
            name: course.content,
            sequence: index,
          })),
    ],
  };

  axios
      .put('https://updateProgramAPI/')
      .then((response) => console.log(response))
      .catch((err) => console.log(err));
};

But MongoDB is showing the following error:

code: 11000,
codeName: 'DuplicateKey',
keyValue: { 'courses.sequence': 1 },
'$clusterTime': {
  clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1637694265 },
  signature: { hash: [Binary], keyId: [Long] }
}

The database isn't getting updated.

about 4 years ago · Juan Pablo Isaza
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!