{"id":15491576,"url":"https://github.com/diegohaz/mongoose-create-unique","last_synced_at":"2025-08-18T11:44:03.568Z","repository":{"id":57301987,"uuid":"57938030","full_name":"diegohaz/mongoose-create-unique","owner":"diegohaz","description":"Mongoose plugin to create a document or return the existing one based on the unique index","archived":false,"fork":false,"pushed_at":"2017-07-15T02:20:33.000Z","size":45,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-16T09:29:08.547Z","etag":null,"topics":["mongodb","mongoose","plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diegohaz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-03T03:15:20.000Z","updated_at":"2018-02-17T03:04:43.000Z","dependencies_parsed_at":"2022-09-07T00:10:25.150Z","dependency_job_id":null,"html_url":"https://github.com/diegohaz/mongoose-create-unique","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fmongoose-create-unique","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fmongoose-create-unique/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fmongoose-create-unique/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fmongoose-create-unique/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diegohaz","download_url":"https://codeload.github.com/diegohaz/mongoose-create-unique/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250306735,"owners_count":21408955,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongodb","mongoose","plugin"],"created_at":"2024-10-02T07:54:18.529Z","updated_at":"2025-04-22T19:22:08.834Z","avatar_url":"https://github.com/diegohaz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-create-unique\n\n[![JS Standard Style][standard-image]][standard-url]\n[![NPM version][npm-image]][npm-url]\n[![Build Status][travis-image]][travis-url]\n[![Coveralls Status][coveralls-image]][coveralls-url]\n[![Dependency Status][depstat-image]][depstat-url]\n[![Downloads][download-badge]][npm-url]\n\n\u003e Mongoose plugin to create a document or return the existing one based on the unique index\n\n## Install\n\n```sh\nnpm install --save mongoose-create-unique\n```\n\nIt works with MongoDB 3.0 or higher.\n\n## The problem\n\nIf you try to create a document with a duplicate key, MongoDB and Mongoose will throw the following error:\n\u003e E11000 duplicate key error collection: mydb.mycollection index: myfield_1 dup key: { : \"my value\" }'\n\nWe want to avoid this error when creating a new document with unique field by returning the existing one.\n\n## How it works\n\nIt was designed to work the same way [`Model.create`](http://mongoosejs.com/docs/api.html#model_Model.create) and [`Model#save`](http://mongoosejs.com/docs/api.html#model_Model-save) do. Just use `Model.createUnique` and `Model#saveUnique` instead. The only difference is that it will return the existing document(s) if there is already one, not an error.\n\nWhat `mongoose-create-unique` actually does is try to save the document(s). If Mongo throw the duplicate key error, it finds the existing document and returns it.\n\n## Example\n```js\nvar mongoose = require('mongoose');\nmongoose.plugin(require('mongoose-create-unique'));\n\nvar ArtistSchema = new mongoose.Schema({\n  name: {\n    type: String,\n    unique: true\n  }\n});\n\nvar Artist = mongoose.model('Artist', ArtistSchema);\n\nArtist.createUnique({name: 'Shakira'}).then(function(artist) {\n  console.log(artist); // {_id: 1, name: 'Shakira'}\n  return Artist.createUnique({name: 'Rihanna'});\n}).then(function(artist) {\n  console.log(artist); // {_id: 2, name: 'Rihanna'}\n  return Artist.createUnique({name: 'Shakira'});\n}).then(function(artist) {\n  console.log(artist); // {_id: 1, name: 'Shakira'}\n});\n\n// or multiple\nArtist.createUnique(\n  {name: 'Shakira'},\n  {name: 'Rihanna'},\n  {name: 'Shakira'}\n).then(function(artists) {\n  // artists[0] and artists[2] are the same  \n})\n```\n\n## License\n\nMIT © [Diego Haz](http://github.com/diegohaz)\n\n[standard-url]: http://standardjs.com\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n\n[npm-url]: https://npmjs.org/package/mongoose-create-unique\n[npm-image]: https://img.shields.io/npm/v/mongoose-create-unique.svg?style=flat-square\n\n[travis-url]: https://travis-ci.org/diegohaz/mongoose-create-unique\n[travis-image]: https://img.shields.io/travis/diegohaz/mongoose-create-unique.svg?style=flat-square\n\n[coveralls-url]: https://coveralls.io/r/diegohaz/mongoose-create-unique\n[coveralls-image]: https://img.shields.io/coveralls/diegohaz/mongoose-create-unique.svg?style=flat-square\n\n[depstat-url]: https://david-dm.org/diegohaz/mongoose-create-unique\n[depstat-image]: https://david-dm.org/diegohaz/mongoose-create-unique.svg?style=flat-square\n\n[download-badge]: http://img.shields.io/npm/dm/mongoose-create-unique.svg?style=flat-square\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiegohaz%2Fmongoose-create-unique","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiegohaz%2Fmongoose-create-unique","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiegohaz%2Fmongoose-create-unique/lists"}