{"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":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-21T17:19:28Z","updated_at":"2015-09-21T20:18:53Z","closed_at":null,"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/142097381","html_url":"https://github.com/Automattic/mongoose/issues/3393#issuecomment-142097381","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3393","id":142097381,"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-21T20:18:53Z","updated_at":"2015-09-21T20:18:53Z","body":"Thanks @vkarpov15,\r\n\r\nI've added the index event listener to the end of the schema file like so:\r\n```js\r\nvar Client = module.exports = mongoose.model('Client', ClientSchema)\r\nClient.on('index', function (error) {\r\n    console.log(\"ON INDEX\")\r\n    if (error) console.log(error)\r\n  })\r\n```\r\nThe listener is working (\"ON INDEX\" gets printed to the console) but ```error``` is empty.\r\n\r\nI've also added a generic error listener on the mongo connection like so:\r\n```js\r\n...\r\nmongoose.connect(process.env.MONGO_URL)\r\nmongoose.connection.on('error', function (err) {\r\n  console.log('MONGOOSE ERROR')\r\n  console.log(err)\r\n})\r\n```\r\n...and there aren't any errors being printed out.\r\n\r\nRegardless of what I do, ```db.getCollection('clients').getIndexes()``` always seems to return:\r\n```json\r\n{\r\n    \"0\" : {\r\n        \"v\" : 1,\r\n        \"key\" : {\r\n            \"_id\" : 1\r\n        },\r\n        \"name\" : \"_id_\",\r\n        \"ns\" : \"vpr.clients\"\r\n    }\r\n}\r\n```\r\n...unless I create the compound index manually via the Mongo shell as I mentioned above.\r\n\r\nAny other thoughts?"}}