mongodb-autoincrement icon indicating copy to clipboard operation
mongodb-autoincrement copied to clipboard

duplicate _id error will get dead loop, which also throw 11000

Open futurist opened this issue 7 years ago • 1 comments

Below line have bug:

                if (err.code == 11000) {
                    process.nextTick(getNextId.bind(null, db, collectionName, fieldName, callback));
                } else {
                    callback(err);
                }

The 11000 error also generated when duplicate _id inserted, please see the PR #7

Hope it can be merged!

futurist avatar Jun 02 '17 01:06 futurist

+1

I am getting the same issue:

There occured some error : WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error collection: auth.consumers index: _id_ dup key: { : 400031 }

Details:

I imported/synced some data which already existed in previous DB. And now, It is trying to create ID:400031 while IDs till 400079 already exist.

So each time I hit the creation API, the insertion ID (which the module tries to insert into DB) is increased by 1 and if the ID is not there, the insertion occurs successfully but if ID is already existing, it throws the above error.

Conclusion: NoSQL does not guarantee ACID properties and these properties need be ensured manually and with this module, it is highly required to check for consistency.

Suggested Solution: So if there exist the ID and if "code":11000 is received in the error response from mongo, Module should check for the highest possible existing ID and should set the key 'count' in the model object of collection 'identitycounters'.

E.g. In my case highest existing ID is 400281 while the count = 400039 in 'Consumer' Model object of collection: 'idenitycounters' so the problem is occurring. As soon as this error is thrown, if the count is reset to 400281, it will not create any more trouble.

akashdeepsinghal avatar Jun 02 '17 07:06 akashdeepsinghal