{"id":16731459,"url":"https://github.com/skratchdot/mongodb-distinct2","last_synced_at":"2025-03-15T18:22:25.858Z","repository":{"id":4291797,"uuid":"5422145","full_name":"skratchdot/mongodb-distinct2","owner":"skratchdot","description":"Similar to the built-in distinct() function, but with more capabilities.","archived":false,"fork":false,"pushed_at":"2013-03-08T01:30:54.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T08:09:55.504Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skratchdot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-15T04:48:26.000Z","updated_at":"2013-03-08T01:30:54.000Z","dependencies_parsed_at":"2022-09-25T02:22:53.889Z","dependency_job_id":null,"html_url":"https://github.com/skratchdot/mongodb-distinct2","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-distinct2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-distinct2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-distinct2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-distinct2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skratchdot","download_url":"https://codeload.github.com/skratchdot/mongodb-distinct2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243771057,"owners_count":20345393,"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":[],"created_at":"2024-10-12T23:37:24.430Z","updated_at":"2025-03-15T18:22:25.822Z","avatar_url":"https://github.com/skratchdot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MongoDB - distinct2.js #\n\n[Project Page](http://skratchdot.com/projects/mongodb-distinct2/)  \n[Source Code](https://github.com/skratchdot/mongodb-distinct2/)  \n[Issues](https://github.com/skratchdot/mongodb-distinct2/issues/)  \n\n## Description: ##\n\nSimilar to the db.myCollection.distinct() function, distinct2() allows\nyou to pass in an array of keys to get values for.  It also allows you\nto pass in an optional \"count\" argument.  When true, you can easily get\nthe counts for your distinct values.\n\nYou can also call distinct2() on the results of a query (something you\ncan't currently do with the built in distinct() function).\n\nTo accomplish this, it adds the following functions to our built in mongo prototypes:  \n\n```javascript\nDBCollection.prototype.distinct2 = function (keys, count) {};\nDBQuery.prototype.distinct2 = function (keys, count) {};\n```\n\n## Usage: ##\n\n```javascript\n// All 5 of these statements are the same as:\n//\n//     db.users.distinct('name.first')\n//\ndb.users.distinct2('name.first');\ndb.users.distinct2('name.first', false);\ndb.users.distinct2(['name.first']);\ndb.users.distinct2(['name.first'], false);\ndb.users.find().distinct2('name.first');\n\n// you can pass in an array of values\ndb.users.distinct2(['name.first','name.last']);\n\n// you can get counts\ndb.users.distinct2('name.first', true);\n\n// you can get distinct values from the results of a query\ndb.users.find({'name.first':'Bob'}).distinct2('name.last');\n\n//\n// When calling distinct2() on large datasets, status on the operation\n// will be printed to the shell. By default, the status interval is 1000ms.\n// If you would like more/less frequent updates, you can pass in an interval\n// time to a setStatusInterval() function. If you would like to disable\n// status updates, you can pass in a negative number.\n//\n\n// Print status updates every 5 seconds:\ndb.users.distinct2.setStatusInterval(5000);\n\n// Disable status updates\ndb.users.distinct2.setStatusInterval(0);\n\n// distinct2 can be used like group(), but has a simpler syntax.\n// Take for instance the following 2 queries (which return the same\n// results- but in a slightly different format). Let's say the users\n// collection has 37 documents. All users have a first name of either\n// 'Bob' or 'Amy'. Compare these 2 queries (and results):\n\n// query 1\ndb.users.distinct2(\"name.first\", true);\n// result 1\n[\n\t[ \"Bob\", 22 ],\n\t[ \"Amy\", 15 ]\n]\n\n// query 2\ndb.users.group({\n\tkey : { \"name.first\" : 1 },\n\t$reduce : function (curr, result) { result.total++; },\n\tinitial : { total : 0 }\n});\n// result 2\n[\n\t{ \"first.name\" : \"Bob\", \"total\" : 22 },\n\t{ \"first.name\" : \"Amy\", \"total\" : 15 }\n]\n\n```\n\n## Installation: ##\n\nDownload: [distinct2.js](https://github.com/skratchdot/mongodb-distinct2/raw/master/distinct2.js)\n\n### Option 1 ###\n\nAdd this script to your .mongorc.js file.  \n\nSee: [http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-.mongorc.js](http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-.mongorc.js)\n\n### Option 2 ###\n\nStart the shell after executing this script  \n\n    mongo --shell distinct2.js\n\n### Disclaimer\n\nThis is not a highly efficient function. Use caution when running this on large\ncollections.  It works great on smaller datasets, but may be unusable on huge\ncollections.  There is a status line that will be printed every 1000ms. You can always\nctrl-c if it takes too long.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskratchdot%2Fmongodb-distinct2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskratchdot%2Fmongodb-distinct2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskratchdot%2Fmongodb-distinct2/lists"}