{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3393","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3393/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3393/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3393/events","html_url":"https://github.com/Automattic/mongoose/issues/3393","id":107558265,"number":3393,"title":"Schema-level compound indexes not being saved to Mongo","user":{"login":"docksteaderluke","id":1275275,"avatar_url":"https://avatars.githubusercontent.com/u/1275275?v=3","gravatar_id":"","url":"https://api.github.com/users/docksteaderluke","html_url":"https://github.com/docksteaderluke","followers_url":"https://api.github.com/users/docksteaderluke/followers","following_url":"https://api.github.com/users/docksteaderluke/following{/other_user}","gists_url":"https://api.github.com/users/docksteaderluke/gists{/gist_id}","starred_url":"https://api.github.com/users/docksteaderluke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docksteaderluke/subscriptions","organizations_url":"https://api.github.com/users/docksteaderluke/orgs","repos_url":"https://api.github.com/users/docksteaderluke/repos","events_url":"https://api.github.com/users/docksteaderluke/events{/privacy}","received_events_url":"https://api.github.com/users/docksteaderluke/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-09-21T17:19:28Z","updated_at":"2015-09-23T14:57:45Z","closed_at":"2015-09-23T14:26:15Z","body":"Hi,\r\n\r\nI'm trying to set a unique compound index on two fields in a schema. Here's a shortened version of my Schema file:\r\n```js\r\n'use strict'\r\n\r\n// Require\r\nvar mongoose = require('mongoose')\r\n  , uniqueValidator = require('mongoose-unique-validator')\r\n  , validate = require('mongoose-validator')\r\n\r\n// Variables\r\nvar Schema = mongoose.Schema\r\n  , ClientSchema = new Schema(\r\n    {\r\n      name : {\r\n        type     : String,\r\n        required : true,\r\n        trim     : true,\r\n        validate : [\r\n          validate({\r\n            validator : 'isLength',\r\n            arguments : [ 1, 100 ],\r\n            message   : 'Name must be 1-100 characters long.'\r\n          })\r\n        ]\r\n      },\r\n      organization : {\r\n        type     : Schema.Types.ObjectId,\r\n        ref      : 'Organization',\r\n        required : true\r\n      }\r\n    }\r\n  )\r\n\r\n// Indexes\r\nClientSchema.index({ \"name\" : 1, \"organization\" : 1 }, { unique : true })\r\n\r\n// Plugins\r\nClientSchema.plugin(uniqueValidator, {\r\n  message : 'Name must be unique.'\r\n})\r\n\r\nmodule.exports = mongoose.model('Client', ClientSchema)\r\n```\r\n\r\nFor some reason, even after completely dropping the collection, the compound index doesn't get added to mongo at all. However, if I create the index in the Mongo shell it works:\r\n```\r\ndb.clients.ensureIndex({\"name\":1,\"organization\":1}, {unique:true})\r\n```\r\n\r\nI've been scouring the Internet and reading through the documentation and can't seem to figure out why this isn't working. Any ideas?\r\n\r\nI'm using:\r\n- Mongo v3.0.6\r\n- Mongoose 4.1.7\r\n- Node 0.12.7"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/142628778","html_url":"https://github.com/Automattic/mongoose/issues/3393#issuecomment-142628778","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3393","id":142628778,"user":{"login":"docksteaderluke","id":1275275,"avatar_url":"https://avatars.githubusercontent.com/u/1275275?v=3","gravatar_id":"","url":"https://api.github.com/users/docksteaderluke","html_url":"https://github.com/docksteaderluke","followers_url":"https://api.github.com/users/docksteaderluke/followers","following_url":"https://api.github.com/users/docksteaderluke/following{/other_user}","gists_url":"https://api.github.com/users/docksteaderluke/gists{/gist_id}","starred_url":"https://api.github.com/users/docksteaderluke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docksteaderluke/subscriptions","organizations_url":"https://api.github.com/users/docksteaderluke/orgs","repos_url":"https://api.github.com/users/docksteaderluke/repos","events_url":"https://api.github.com/users/docksteaderluke/events{/privacy}","received_events_url":"https://api.github.com/users/docksteaderluke/received_events","type":"User","site_admin":false},"created_at":"2015-09-23T14:57:45Z","updated_at":"2015-09-23T14:57:45Z","body":"Also, thanks for the heads up about mongoose-unique-validator. I was using it to build more user-friendly error messages. Any suggestions on a better way to do this than outputting:\r\n```E11000 duplicate key error index: vpr.organizations.$name 1 dup key: { : \\\"timeless veterinary systems, inc.\\\" }```"}}