{"id":16731452,"url":"https://github.com/skratchdot/mongodb-schema","last_synced_at":"2025-04-10T11:20:44.386Z","repository":{"id":3158886,"uuid":"4189290","full_name":"skratchdot/mongodb-schema","owner":"skratchdot","description":"A schema analysis tool for MongoDB","archived":false,"fork":false,"pushed_at":"2016-01-04T21:07:32.000Z","size":14,"stargazers_count":28,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T10:06:36.487Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://skratchdot.com/projects/mongodb-schema/","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:17:13.000Z","updated_at":"2021-05-18T23:49:57.000Z","dependencies_parsed_at":"2022-08-30T13:11:00.957Z","dependency_job_id":null,"html_url":"https://github.com/skratchdot/mongodb-schema","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skratchdot%2Fmongodb-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skratchdot","download_url":"https://codeload.github.com/skratchdot/mongodb-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248208602,"owners_count":21065203,"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:20.020Z","updated_at":"2025-04-10T11:20:44.369Z","avatar_url":"https://github.com/skratchdot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MongoDB - schema.js #\r\n\r\n[Project Page](http://skratchdot.com/projects/mongodb-schema/)  \r\n[Source Code](https://github.com/skratchdot/mongodb-schema/)  \r\n[Issues](https://github.com/skratchdot/mongodb-schema/issues/)  \r\n\r\n## Description: ##\r\n\r\nThis is a schema analysis tool for MongoDB. It accomplishes this by\r\nextending the mongo shell, and providing a new function called schema()\r\nwith the following signature:  \r\n\r\n```javascript\r\nDBCollection.prototype.schema = function (optionsOrOutString)  \r\n```\r\n\r\nThe schema() function accepts all the same parameters that the mapReduce() function\r\ndoes. It adds/modifies the following 4 parameters that can be used as well:\r\n\r\n    wildcards - array (default: [])\r\n        By using the $, you can combine report results.\r\n        For instance: '$' will group all top level keys and\r\n        'foo.$.bar' will combine 'foo.baz.bar' and 'foo.bar.bar'\r\n\r\n    arraysAreWildcards - boolean (default: true)\r\n        When true, 'foo.0.bar' and 'foo.1.bar' will be\r\n        combined into 'foo.$.bar'\r\n        When false, all array keys will be reported\r\n\r\n    fields - object (default: {})\r\n        Similar to the usage in find(). You can pick the\r\n        fields to include or exclude. Currently, you cannot \r\n        pass in nested structures, you need to pass in dot notation keys.\r\n\r\n    limit - number (default: 50)\r\n        Behaves the same as the limit in mapReduce(), but defaults to 50.\r\n        You can pass in 0 or -1 to process all documents.\r\n\r\n## Usage: ##\r\n\r\n```javascript\r\n// Return schema results inline\r\ndb.users.schema();\r\n\r\n// Create and store schema results in the 'users_schema' collection\r\ndb.users.schema('users_schema'); // Option 1\r\ndb.users.schema({out:'users_schema'}); // Option 2\r\ndb.users.schema({out:{replace:'users_schema'}}); // Option 3\r\n\r\n// Only report on the key: 'name.first'\r\ndb.users.schema({fields:{'name.first':1}});\r\n\r\n// Report on everything except 'name.first'\r\ndb.users.schema({fields:{'name.first':-1}});\r\n\r\n// Combine the 'name.first' and 'name.last' keys into 'name.$'\r\ndb.users.schema({wildcards:['name.$']});\r\n\r\n// Don't treat arrays as a wildcard\r\ndb.users.schema({arraysAreWildcards:false});\r\n\r\n// Process 50 documents\r\ndb.users.schema();\r\n\r\n// Process all documents\r\ndb.users.schema({limit:-1});\r\n```\r\n\r\n## Example Result Set: ##\r\n\r\n```javascript\r\n\u003e // Start fresh with a new collection called 'users'\r\n\u003e db.users.remove();\r\n\u003e \r\n\u003e // Add a few records with different schemas\r\n\u003e db.users.insert({'name' : {'first' : 'John', 'last' : 'Smith'}, 'isRegistered' : false, 'tags' : ['male']});\r\n\u003e db.users.insert({'name' : {'first' : 'Bob', 'last' : 'Smith'}, 'isRegistered' : false, 'tags' : ['male','new']});\r\n\u003e db.users.insert({'name' : {'first' : 'Amy', 'last' : 'Smart'}, 'isRegistered' : 1, 'tags' : ['female']});\r\n\u003e db.users.insert({'name' : 'Bob Smith', 'isRegistered' : '0', 'tags' : ['male']});\r\n\u003e \r\n\u003e // Print our results to the console\r\n\u003e db.users.schema();\r\nProcessing 4 document(s)...\r\n{\r\n  \"results\" : [\r\n    {\r\n      \"_id\" : \"_id\",\r\n      \"value\" : {\r\n        \"wildcard\" : false,\r\n        \"types\" : [\"objectid\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1},\r\n          {\"type\" : \"objectid\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1}\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"_id\" : \"isRegistered\",\r\n      \"value\" : {\r\n        \"wildcard\" : false,\r\n        \"types\" : [\"boolean\",\"number\",\"string\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1},\r\n          {\"type\" : \"boolean\",\"docs\" : 2,\"coverage\" : 50,\"perDoc\" : 1},\r\n          {\"type\" : \"number\",\"docs\" : 1,\"coverage\" : 25,\"perDoc\" : 1},\r\n          {\"type\" : \"string\",\"docs\" : 1,\"coverage\" : 25,\"perDoc\" : 1}\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"_id\" : \"name\",\r\n      \"value\" : {\r\n        \"wildcard\" : false,\r\n        \"types\" : [\"bson\",\"string\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1},\r\n          {\"type\" : \"bson\",\"docs\" : 3,\"coverage\" : 75,\"perDoc\" : 1},\r\n          {\"type\" : \"string\",\"docs\" : 1,\"coverage\" : 25,\"perDoc\" : 1}\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"_id\" : \"name.first\",\r\n      \"value\" : {\r\n        \"wildcard\" : false,\r\n        \"types\" : [\"string\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 3,\"coverage\" : 75,\"perDoc\" : 1},\r\n          {\"type\" : \"string\",\"docs\" : 3,\"coverage\" : 75,\"perDoc\" : 1}\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"_id\" : \"name.last\",\r\n      \"value\" : {\r\n        \"wildcard\" : false,\r\n        \"types\" : [\"string\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 3,\"coverage\" : 75,\"perDoc\" : 1},\r\n          {\"type\" : \"string\",\"docs\" : 3,\"coverage\" : 75,\"perDoc\" : 1}\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"_id\" : \"tags\",\r\n      \"value\" : {\r\n        \"wildcard\" : false,\r\n        \"types\" : [\"array\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1},\r\n          {\"type\" : \"array\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1}\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"_id\" : \"tags.$\",\r\n      \"value\" : {\r\n        \"wildcard\" : true,\r\n        \"types\" : [\"string\"],\r\n        \"results\" : [\r\n          {\"type\" : \"all\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1.25},\r\n          {\"type\" : \"string\",\"docs\" : 4,\"coverage\" : 100,\"perDoc\" : 1.25}\r\n        ]\r\n      }\r\n    }\r\n  ],\r\n  \"timeMillis\" : 16,\r\n  \"counts\" : {\"input\" : 4,\"emit\" : 26,\"reduce\" : 7,\"output\" : 7},\r\n  \"ok\" : 1,\r\n}\r\n```\r\n\r\n## Caveats: ##\r\n\r\nBy design, schema() returns 'bson' rather than 'object'.\r\nIt will return 'numberlong' rather than 'number', etc.\r\n\r\n## Installation: ##\r\n\r\nDownload: [schema.js](https://github.com/skratchdot/mongodb-schema/raw/master/schema.js)\r\n\r\n### Option 1 ###\r\n\r\nAdd this script to your .mongorc.js file.  \r\n\r\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)\r\n\r\n### Option 2 ###\r\n\r\nStart the shell after executing this script  \r\n\r\n    mongo --shell schema.js\r\n\r\n## Inspired by: ##\r\n\r\nVariety: [https://github.com/JamesCropcho/variety](https://github.com/JamesCropcho/variety)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskratchdot%2Fmongodb-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskratchdot%2Fmongodb-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskratchdot%2Fmongodb-schema/lists"}