Duplicate key error index


#1

I’m getting intermittent errors on an app that launched today that I can’t make happen in development. Happens about .05% of the time. Running multiple servers at Modulus with a Compose.io database.

Any ideas? Here’s the error trace from Kadira.

MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: flt-easter.entries.$_id_  dup key: { : "cD9YqcKNGCvwgRipr" }
    at Object.Future.wait (/mnt/data/1/node_modules/fibers/future.js:326:15)
    at [object Object].<anonymous> (packages/meteor/helpers.js:119:1)
    at [object Object].MongoConnection.(anonymous function) (packages/mongo/mongo_driver.js:634:1)
    at [object Object].mongoConnectionProto.(anonymous function) [as insert] (packages/meteorhacks:kadira/lib/hijack/db.js:58:1)
    at OptimizedApply (packages/meteorhacks:kadira/lib/utils.js:31:1)
    at Object.ret.(anonymous function) [as insert] (packages/meteorhacks:kadira/lib/hijack/db.js:16:1)
    at [object Object].Mongo.Collection.(anonymous function) (packages/mongo/collection.js:574:1)
    at [object Object].Mongo.Collection.(anonymous function) [as insert] (packages/aldeed:collection2/collection2.js:159:1)
    at [object Object].Meteor.methods.entryInsert (app/collections/entry.js:60:27)
    at [object Object].methodMap.(anonymous function) (packages/meteorhacks:kadira/lib/hijack/wrap_session.js:160:1)

#2

Can you share the places in your code where you insert into the entries collection?


#3

template.js

Meteor.call('entryInsert', entry, function(error, result) {
      // display the error to the user and abort
      if (error)
        return throwError(error.reason);

      if (result.correctAnswer == false) {
        Router.go('contestNotCorrectThankYou');
      }
      else
      {
        Router.go('contestCorrectThankYou');
      }
    });
  }
});

Meteor.methods({
  entryInsert: function(entryAttributes) {
    check(entryAttributes, {
      answer: String,
      first_name: String,
      last_name: String,
      email: String,
      phone: String,
      subscribe_contests: Boolean,
      subscribe_general: Boolean,
      legal_age: Boolean,
      agree_rules: Boolean,
      contest_id: String
    });

    var errors = validateEntry(entryAttributes);
    if (errors.answer || errors.first_name || errors.last_name || errors.email || errors.phone || errors.legal_age || errors.agree_rules)
      throw new Meteor.Error('invalid-entry', "You must enter all of the information to be entered.");


    var contestEntered = Contests.findOne({_id: entryAttributes.contest_id});

    var lowerCaseNames = contestEntered.answers.map(function(value) {
      return value.toLowerCase();
    });

    if(lowerCaseNames.indexOf (entryAttributes.answer.trim().toLowerCase()) != -1){
      var entry = _.extend(entryAttributes, {
        correctAnswer: true,
        submitted: new Date()
      });
    } else {
      var entry = _.extend(entryAttributes, {
        correctAnswer: false,
        submitted: new Date()
      });
    }

    var entryId = Entries.insert(entry);

… some mailchimp stuff

return {
      _id: entryId,
      correctAnswer: entry.correctAnswer
    };
  }
});

#4

Hmm, is the method defined in a server file or a file that is available to both the server and the client?


#5

oops sorry, ya the methods and under is on the server side.


#6

actually its under collections/something.js


#7

At first, this sounded to me like an intermittent sticky sessions
issue, but your method runs only on the server, which means there is no
id generation on the client. So it cannot be the method being called
twice from the client with the same id.

Meteor’s id’s are theoretically less likely to be unique than those
that are generated natively by mongo, but again that still would be one
in millions, not 0.05%

All your code execute synchronously/blocking so it is not a
synchronicity issue.

Very puzzling, indeed.

I’m sorry I could not help, but sure am interested in what you’ll find
it out to be.


#8

the errors seemed to have stopped. Nothing in the last 8 hours. Weird. Thanks for your help.


#9

I am also getting a same error since last two days.
Did you get any solution to it?


#10

I got the solution, I have used wrapAsync metohod with callback and it’s work for me.