Meteor - duplicate key error?

Hey,
on my production environment, I get a lot of errors like this on my server:

Exception while invoking method 'addMessage' MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: 

This is the method I’m using, it available on client and server, to have an optimistic ui:

'addMessage': function (message) {
    check(message, String);
    var currentUser = Meteor.users.findOne(this.userId);

    Messages.insert({
        text: message,
        chatId: currentUser.profile.chatId,
        userId: this.userId,
        createdAt: new Date()
    });
}

I’m running multiple instances and the database has more than 200k entries in it. Why do I get this error? The only issues I’ve seen happened when a user set the _id property manually, but in my case Meteor should generate unique ids for every entry? Is there any workaround for this, f.e. using try catch?

It happens for me from time to time and it always mean one thing - the collection’s schema (Simple Schema for me) is missing some of the keys that I try to insert into this document. Please check if it’s not a case for you.

I’ve to say that it was my first Meteor app back in August 15 and I didn’t use Simple Schema for it :smiley:. Also I didn’t define any indexes for this collection. So it’s really only

Messages = new Meteor.Collection("messages");
1 Like

So any other ideas for this? Do I really have to remove the optimistic UI and only define the method on the server side?

The other question is why does it happen, and why Meteor isn’t handling it automatically if it’s automatic IDs are duplicates.

Hi. Same problem here, any solution ?

Nope, not a real one. Moving the method from client/server to server only solved the issue, but then you don’t have a optimistic UI on client side.

I guess that the client is executing the Message.insert twice sometimes, which will end up in a duplicate key error. Maybe it’s a connection issue. The client “thinks” that the Message.insert DDP call wasn’t sent successfully and executes it again.

I really don’t understand how meteor can have this problem and don’t handle it. Even in their official todos demo app (https://github.com/meteor/todos/) they call their insert method inside their file api/lists/methods.js and which is executed on both client and server side.

Are you using the cluster package? If not, we may should reopen this issue here:

No I’m not using the cluster package

Okay, maybe you should create a new issue on GitHub and link the old one so that one of the Meteor core team can check this again. In my case they thought that the cluster module caused the issue, if you are not using it, it must be something else.

I seem to get the same error. I do not use cluster.
A note is that my server does not support websockets.

This is my error log:

Exception while invoking method 'log' WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error collection: admin.Logs index: _id_ dup key: { : \"H5h8oBZnxABctfysS\" }","op":{ ... }})
    at Object.Future.wait (/data/home/l_meteor_maw/bundle/programs/server/node_modules/fibers/future.js:449:15)
    at MongoConnection.<anonymous> (packages/meteor.js:213:24)
    at MongoConnection.(anonymous function) [as insert] (packages/mongo/mongo_driver.js:774:49)
    at [object Object].insert (packages/mongo/collection.js:528:37)
    at [object Object].Mongo.Collection.(anonymous function) [as insert] (packages/aldeed_collection2-core.js:232:19)
    at [object Object].run (imports/api/logs/methods.js:21:8)
    at ValidatedMethod._execute (packages/mdg:validated-method/validated-method.js:93:12)
    at [object Object].ValidatedMethod.connection.methods._connection$methods.(anonymous function) (packages/mdg:validated-method/validated-method.js:54:23)
    at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1712:12)
    at packages/ddp-server/livedata_server.js:711:19
    - - - - -
    at Function.MongoError.create (/data/home/l_meteor_maw/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/data/home/l_meteor_maw/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/utils.js:115:22)
    at /data/home/l_meteor_maw/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/collection.js:656:23
    at handleCallback (/data/home/l_meteor_maw/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/utils.js:96:56)
    at resultHandler (/data/home/l_meteor_maw/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/bulk/ordered.js:428:14)
    at /data/home/l_meteor_maw/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:455:18
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

Timestamp for when the insertion was made in the collection: 1488373430949
Timestamp for when the error is triggered: 1488373432561

Where did you define your method “log”? Is it available on server only or client + server?

It is available on both.

Okay, best way is to comment it on my opened GitHub issue:

This error only happens if you define the method on client and server. If you don’t need optimistic UI for your log method, you can move it to server only. This will solve your issue.

Alright! I can do without it on the client, so I’ll try removing it, if it keeps happening I’ll update again.
Thank you!