{"action":"opened","issue":{"url":"https://api.github.com/repos/meteor/meteor/issues/6637","repository_url":"https://api.github.com/repos/meteor/meteor","labels_url":"https://api.github.com/repos/meteor/meteor/issues/6637/labels{/name}","comments_url":"https://api.github.com/repos/meteor/meteor/issues/6637/comments","events_url":"https://api.github.com/repos/meteor/meteor/issues/6637/events","html_url":"https://github.com/meteor/meteor/issues/6637","id":144549489,"number":6637,"title":"Duplicate key error","user":{"login":"dnish","id":7636313,"avatar_url":"https://avatars.githubusercontent.com/u/7636313?v=3","gravatar_id":"","url":"https://api.github.com/users/dnish","html_url":"https://github.com/dnish","followers_url":"https://api.github.com/users/dnish/followers","following_url":"https://api.github.com/users/dnish/following{/other_user}","gists_url":"https://api.github.com/users/dnish/gists{/gist_id}","starred_url":"https://api.github.com/users/dnish/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dnish/subscriptions","organizations_url":"https://api.github.com/users/dnish/orgs","repos_url":"https://api.github.com/users/dnish/repos","events_url":"https://api.github.com/users/dnish/events{/privacy}","received_events_url":"https://api.github.com/users/dnish/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-30T11:01:59Z","updated_at":"2016-03-30T11:01:59Z","closed_at":null,"body":"I'm getting an exception that crashes my whole application if a user inserts a document to my collection:\r\n\r\n    Exception while invoking method 'addMessage' MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index:\r\n\r\n\r\nThis is the method, defined in **lib/methods/messages.js**\r\n\r\n```\r\n'addMessage': function (message) {\r\n    check(message, String);\r\n    var currentUser = Meteor.users.findOne(this.userId);\r\n\r\n    Messages.insert({\r\n        text: message,\r\n        chatId: currentUser.profile.chatId,\r\n        userId: this.userId,\r\n        createdAt: new Date()\r\n    });\r\n}\r\n```\r\n\r\nCollection is defined in **lib/collections/messages.js**\r\n\r\n```\r\nMessages = new Meteor.Collection(\"messages\");\r\n\r\nif(Meteor.isServer) {\r\n    Messages._ensureIndex({ \"chatId\": 1, \"createdAt\":-1});\r\n}\r\n```\r\n\r\nThis error doesn't happen every time, but sometimes, ending in crashing the whole worker (I'm using meteor-cluster with 4 workers on a single server). The collection has about 40k+ inserts daily."}}