{"action":"created","issue":{"url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/223","labels_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/223/labels{/name}","comments_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/223/comments","events_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/223/events","html_url":"https://github.com/aldeed/meteor-collection2/pull/223","id":76450553,"number":223,"title":"Expose `sparse` option for indexing","user":{"login":"lourd","id":2012388,"avatar_url":"https://avatars.githubusercontent.com/u/2012388?v=3","gravatar_id":"","url":"https://api.github.com/users/lourd","html_url":"https://github.com/lourd","followers_url":"https://api.github.com/users/lourd/followers","following_url":"https://api.github.com/users/lourd/following{/other_user}","gists_url":"https://api.github.com/users/lourd/gists{/gist_id}","starred_url":"https://api.github.com/users/lourd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lourd/subscriptions","organizations_url":"https://api.github.com/users/lourd/orgs","repos_url":"https://api.github.com/users/lourd/repos","events_url":"https://api.github.com/users/lourd/events{/privacy}","received_events_url":"https://api.github.com/users/lourd/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-14T18:37:18Z","updated_at":"2015-05-20T13:34:10Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/aldeed/meteor-collection2/pulls/223","html_url":"https://github.com/aldeed/meteor-collection2/pull/223","diff_url":"https://github.com/aldeed/meteor-collection2/pull/223.diff","patch_url":"https://github.com/aldeed/meteor-collection2/pull/223.patch"},"body":"As the package works now, the following use case fails:\r\n\r\n```js\r\nvar Things = new Mongo.Collection('things');\r\n\r\nvar SmallerThingSchema = new SimpleSchema({\r\n  foo: {\r\n    type: String,\r\n    index: true,\r\n    unique: true\r\n  },\r\n});\r\n\r\nvar ThingSchema = new SimpleSchema({\r\n  name: {\r\n    type: String,\r\n    index: true,\r\n    unique: true\r\n  },\r\n  smallerThings: {\r\n    type: [SmallerThingSchema]\r\n  }\r\n});\r\n\r\nThings.attachSchema(ThingSchema);\r\n\r\nvar thingOne = {\r\n  name: \"Thing One\",\r\n  smallerThings: []\r\n};\r\n\r\nThings.insert(thingOne);\r\n\r\nvar thingTwo = {\r\n  name: \"Thing Two\",\r\n  smallerThings: []\r\n};\r\n\r\nThings.insert(thingTwo);\r\n// -> MongoError: E11000 duplicate key error index: meteor.things.$c2_smallerThings.$.chipId  dup key: { : null }\r\n```\r\n\r\nAs Mongo specifies in [\"Unique Index and Missing Field\"](http://docs.mongodb.org/manual/core/index-unique/#unique-index-and-missing-field) section of their docs, _If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error._\r\n\r\nThe solution to that is building a sparse index. This code exposes `sparse` as a schema option, rather than relying on the `optional` field like before.\r\n\r\nSo now this makes the above use case work\r\n```js\r\nvar SmallerThingSchema = new SimpleSchema({\r\n  foo: {\r\n    type: String,\r\n    index: true,\r\n    unique: true,\r\n    sparse: true\r\n  },\r\n});\r\n```"},"comment":{"url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/comments/103890807","html_url":"https://github.com/aldeed/meteor-collection2/pull/223#issuecomment-103890807","issue_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/223","id":103890807,"user":{"login":"ricardodovalle","id":3634150,"avatar_url":"https://avatars.githubusercontent.com/u/3634150?v=3","gravatar_id":"","url":"https://api.github.com/users/ricardodovalle","html_url":"https://github.com/ricardodovalle","followers_url":"https://api.github.com/users/ricardodovalle/followers","following_url":"https://api.github.com/users/ricardodovalle/following{/other_user}","gists_url":"https://api.github.com/users/ricardodovalle/gists{/gist_id}","starred_url":"https://api.github.com/users/ricardodovalle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ricardodovalle/subscriptions","organizations_url":"https://api.github.com/users/ricardodovalle/orgs","repos_url":"https://api.github.com/users/ricardodovalle/repos","events_url":"https://api.github.com/users/ricardodovalle/events{/privacy}","received_events_url":"https://api.github.com/users/ricardodovalle/received_events","type":"User","site_admin":false},"created_at":"2015-05-20T13:34:10Z","updated_at":"2015-05-20T13:34:10Z","body":":+1:  I need this feature too"}}