{"id":16731456,"url":"https://github.com/skratchdot/mongodb-flatten","last_synced_at":"2025-07-24T22:34:40.734Z","repository":{"id":3158814,"uuid":"4189215","full_name":"skratchdot/mongodb-flatten","owner":"skratchdot","description":"The flatten() function provides a way to flatten documents into id/key/value pairs.","archived":false,"fork":false,"pushed_at":"2012-10-23T04:01:37.000Z","size":128,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T18:24:53.418Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://skratchdot.github.com/mongodb-flatten/","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-05-01T03:03:40.000Z","updated_at":"2016-12-06T11:24:42.000Z","dependencies_parsed_at":"2022-09-20T17:43:42.157Z","dependency_job_id":null,"html_url":"https://github.com/skratchdot/mongodb-flatten","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/skratchdot/mongodb-flatten","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-flatten","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-flatten/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-flatten/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-flatten/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skratchdot","download_url":"https://codeload.github.com/skratchdot/mongodb-flatten/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-flatten/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266913678,"owners_count":24005578,"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","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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.372Z","updated_at":"2025-07-24T22:34:40.705Z","avatar_url":"https://github.com/skratchdot.png","language":"JavaScript","readme":"# MongoDB - flatten.js #\n\n[Project Page](http://skratchdot.com/projects/mongodb-flatten/)  \n[Source Code](https://github.com/skratchdot/mongodb-flatten/)  \n[Issues](https://github.com/skratchdot/mongodb-flatten/issues/)  \n\n## Description: ##\n\nThis project provides a way to flatten documents into id/key/value\npairs. The flatten() function accepts a collectionName parameter.\nThis collection will be emptied, and the newly flattened data will\nbe stored there. If a collectionName is not passed, then a temporary\ncollection will be created with a name in the format: temp.flatten_TIMESTAMP.\n\n## Usage: ##\n\n```javascript\n// Flatten all user documents, and store the results in: temp.flatten_TIMESTAMP\nresult = db.users.flatten();\n\n// Flatten all user documents, and store the result in the users_flattened collection\nresult = db.users.flatten('users_flattened');\n\n// Flatten user documents that have the first name of Bob. Store in a collection\nresult = db.users.find({ 'name.first' : 'Bob' }).flatten('users_flattened');\n\n// Flatten the first 20 user documents into a dynamically named collection\nresult = db.users.find().limit(20).flatten();\n\n// Get the number of keys in one document\ndb.users.find({\n\t'_id' : ObjectId('4f9c2374992274fc8d468675')\n}).flatten().count();\n```\n\n## Example Result Set: ##\n\n```javascript\n\u003e // Start fresh with a new collection called 'users'\n\u003e db.users.remove();\n\u003e \n\u003e // Add a few records with different schemas\n\u003e db.users.insert({'name' : {'first' : 'John', 'last' : 'Smith'}, 'isRegistered' : false, 'tags' : ['male']});\n\u003e db.users.insert({'name' : {'first' : 'Bob', 'last' : 'Smith'}, 'isRegistered' : false, 'tags' : ['male','new']});\n\u003e db.users.insert({'name' : {'first' : 'Amy', 'last' : 'Smart'}, 'isRegistered' : 1, 'tags' : ['female']});\n\u003e db.users.insert({'name' : 'Bob Smith', 'isRegistered' : '0', 'tags' : ['male']});\n\u003e \n\u003e // Print our results to the console\n\u003e db.users.flatten().find();\nFlattening 4 document(s) into the \"temp.flatten_1350767969827\" collection.\n{ \"_id\" : 1, \"i\" : ObjectId(\"5083156186df9e6600186e38\"), \"k\" : \"_id\", \"v\" : ObjectId(\"5083156186df9e6600186e38\") }\n{ \"_id\" : 2, \"i\" : ObjectId(\"5083156186df9e6600186e38\"), \"k\" : \"name.first\", \"v\" : \"John\" }\n{ \"_id\" : 3, \"i\" : ObjectId(\"5083156186df9e6600186e38\"), \"k\" : \"name.last\", \"v\" : \"Smith\" }\n{ \"_id\" : 4, \"i\" : ObjectId(\"5083156186df9e6600186e38\"), \"k\" : \"isRegistered\", \"v\" : false }\n{ \"_id\" : 5, \"i\" : ObjectId(\"5083156186df9e6600186e38\"), \"k\" : \"tags.0\", \"v\" : \"male\" }\n{ \"_id\" : 6, \"i\" : ObjectId(\"5083156186df9e6600186e39\"), \"k\" : \"_id\", \"v\" : ObjectId(\"5083156186df9e6600186e39\") }\n{ \"_id\" : 7, \"i\" : ObjectId(\"5083156186df9e6600186e39\"), \"k\" : \"name.first\", \"v\" : \"Bob\" }\n{ \"_id\" : 8, \"i\" : ObjectId(\"5083156186df9e6600186e39\"), \"k\" : \"name.last\", \"v\" : \"Smith\" }\n{ \"_id\" : 9, \"i\" : ObjectId(\"5083156186df9e6600186e39\"), \"k\" : \"isRegistered\", \"v\" : false }\n{ \"_id\" : 10, \"i\" : ObjectId(\"5083156186df9e6600186e39\"), \"k\" : \"tags.0\", \"v\" : \"male\" }\n{ \"_id\" : 11, \"i\" : ObjectId(\"5083156186df9e6600186e39\"), \"k\" : \"tags.1\", \"v\" : \"new\" }\n{ \"_id\" : 12, \"i\" : ObjectId(\"5083156186df9e6600186e3a\"), \"k\" : \"_id\", \"v\" : ObjectId(\"5083156186df9e6600186e3a\") }\n{ \"_id\" : 13, \"i\" : ObjectId(\"5083156186df9e6600186e3a\"), \"k\" : \"name.first\", \"v\" : \"Amy\" }\n{ \"_id\" : 14, \"i\" : ObjectId(\"5083156186df9e6600186e3a\"), \"k\" : \"name.last\", \"v\" : \"Smart\" }\n{ \"_id\" : 15, \"i\" : ObjectId(\"5083156186df9e6600186e3a\"), \"k\" : \"isRegistered\", \"v\" : 1 }\n{ \"_id\" : 16, \"i\" : ObjectId(\"5083156186df9e6600186e3a\"), \"k\" : \"tags.0\", \"v\" : \"female\" }\n{ \"_id\" : 17, \"i\" : ObjectId(\"5083156186df9e6600186e3b\"), \"k\" : \"_id\", \"v\" : ObjectId(\"5083156186df9e6600186e3b\") }\n{ \"_id\" : 18, \"i\" : ObjectId(\"5083156186df9e6600186e3b\"), \"k\" : \"name\", \"v\" : \"Bob Smith\" }\n{ \"_id\" : 19, \"i\" : ObjectId(\"5083156186df9e6600186e3b\"), \"k\" : \"isRegistered\", \"v\" : \"0\" }\n{ \"_id\" : 20, \"i\" : ObjectId(\"5083156186df9e6600186e3b\"), \"k\" : \"tags.0\", \"v\" : \"male\" }\n```\n\n## Installation: ##\n\nDownload: [flatten.js](https://github.com/skratchdot/mongodb-flatten/raw/master/flatten.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 flatten.js\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskratchdot%2Fmongodb-flatten","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskratchdot%2Fmongodb-flatten","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskratchdot%2Fmongodb-flatten/lists"}