0
votes

I get this error while creating documents in mongodb.

errmsg: 'E11000 duplicate key error collection: db.events index: calendar_1_providerId_1 dup key: { : ObjectId(\'59c58ac77f8b539bacaa49f0\'), : "osv8g39so6q0cptpjhj2kk5ps4_20170602T153000Z" }',

the providerId is already there for another document with different calendarId,but I get this error due to which mongo goes down and server crashes.

eventSchema.index({ 'calendar' : 1, 'providerId': 1 }, { unique: true, sparse: true });

following is the index creation code. Could anyone help me with this? I am really stuck here and went through all possible solutions.

NOTE: the calendar field is not unique only the combination of the calendar and providerId should be unique in the document for instance same providerId can exists in two documents that has different calendar field values and also docs can have same calendar field value with different providerID values.

1
Duplicate key will not cause the server to crash. Are you sure it's the database that crashes and not your app?kevinadi
Yeah the database crashes as the mongo error comes continuously when I hit the Google api.i am using find one and update method to insert the docs with upsert params as true and finding the doc using calendar and providerID parametersakila arasan
I'm not sure I understand. What does Google API have to do with the database crashing? Could you post the relevant MongoDB logs when it crashed, and a description of your setup?kevinadi

1 Answers

0
votes

This error occurs because of you provide { unique: true } for both the keys. It's always checks is there any calendar or providerId already stored in database. if found then it gives error like 'E11000 duplicate key error collection: In your case you need providerId multiple times in single collection so you need to only do this:

eventSchema.index({ 'calendar' : 1 }, { unique: true, sparse: true });