question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get index name on duplicate document 11000 error?

See original GitHub issue

How can I get the index for which the error occurs with?

e.g. if my index is named email, how do I get that from the err object?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
Fire7commented, Feb 17, 2017

Here is my RegExp solution to get index name with any type of error syntax I have found:

  var regex = /index\:\ (?:.*\.)?\$?(?:([_a-z0-9]*)(?:_\d*)|([_a-z0-9]*))\s*dup key/i,      
  match =  error.message.match(regex),  
  indexName = match[1] || match[2];  

Here what I have tested:

('E11000 duplicate key error collection: db.users index: name_1 dup key: { : "Kate" }').match(regex)[1]; // "name"
("E11000 duplicate key error index: myDb.myCollection.$id dup key: { : ObjectId('57226808ec55240c00000272') }").match(regex)[2] // "id"
('E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : null }').match(regex)[1] // "b"
('E11000 duplicate key error collection: upsert_bug.col index: _id_ dup key: { : 3.0 }').match(regex)[1] // "_id"
4reactions
bookercodescommented, Jul 3, 2015

Thanks to @paambaati I got the functionality I needed.

var user = new User(req.body);
  user.save(function(error) {
    if (error) {
      if (error.code === 11000) {
        // email or username could violate the unique index. we need to find out which field it was.
        var field = error.message.split(".$")[1];
        field = field.split(" dup key")[0];
        field = field.substring(0, field.lastIndexOf("_"));
        req.flash("errors", [{
          msg: "An account with this " + field + " already exists."
        }]);
        res.redirect("/join");
        return;
      }
      throw error;
    }
    req.flash("messages", "Thank you for joining.");
    res.redirect("/");
  });

But is there a terser way to get the same result? //cc @vkarpov15

Read more comments on GitHub >

github_iconTop Results From Across the Web

E11000 duplicate key error collection - MongoDB
I have an index in collection “persons” which shall avoid inserting duplicate documents with the same person name.
Read more >
E11000 duplicate key error index in mongodb mongoose
The error message is saying that there's already a record with null as the email. In other words, you already have a user...
Read more >
MongoError: E11000 duplicate key error collection
And once an index is set, it is there until you remove it, and the rule unique is still there also.
Read more >
E11000 duplicate key error index in mongodb mongoose
The error message is saying that there is already a record with null as the email. If a document does not have a...
Read more >
How To Use Indexes in MongoDB - DigitalOcean
MongoDB is a document-oriented database management system that allows you to ... "code" : 11000, "errmsg" : "E11000 duplicate key error ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found