{"id":26358779,"url":"https://github.com/muhammadfarooq85/data-modeling-schema-validation-mongodb","last_synced_at":"2025-03-16T15:28:40.698Z","repository":{"id":282566176,"uuid":"887197023","full_name":"muhammadfarooq85/Data-Modeling-Schema-Validation-MongoDB","owner":"muhammadfarooq85","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-17T15:33:12.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T13:37:13.878Z","etag":null,"topics":["database","mongodb","mongodb-database","no-sql"],"latest_commit_sha":null,"homepage":"","language":null,"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/muhammadfarooq85.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-12T10:33:46.000Z","updated_at":"2024-11-17T15:33:16.000Z","dependencies_parsed_at":"2025-03-15T13:47:22.164Z","dependency_job_id":null,"html_url":"https://github.com/muhammadfarooq85/Data-Modeling-Schema-Validation-MongoDB","commit_stats":null,"previous_names":["muhammadfarooq85/data-modeling-schema-validation-mongodb"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FData-Modeling-Schema-Validation-MongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FData-Modeling-Schema-Validation-MongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FData-Modeling-Schema-Validation-MongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FData-Modeling-Schema-Validation-MongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/muhammadfarooq85","download_url":"https://codeload.github.com/muhammadfarooq85/Data-Modeling-Schema-Validation-MongoDB/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243889751,"owners_count":20364314,"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":["database","mongodb","mongodb-database","no-sql"],"created_at":"2025-03-16T15:28:40.023Z","updated_at":"2025-03-16T15:28:40.692Z","avatar_url":"https://github.com/muhammadfarooq85.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data-Modeling-Schema-Validation-MongoDB\n\n### What is Data Modeling in MongoDb?\n\n#### Data modeling in MongoDB is the process of defining the structure, storage, and organization of data in a database. Unlike traditional relational databases, MongoDB is schema-less, which means it can store documents of varying structures in the same collection. However, it is essential to design a schema that fits your application needs for consistency, efficiency, and performance.\n\n### Key Concepts:\n\n- Documents: Basic units of data in MongoDB stored in BSON format (similar to JSON).\n- Collections: Groupings of documents (akin to tables in relational databases).\n- Embedded Data: Storing related data within a single document.\n- Referenced Data: Storing related data in separate documents with references (similar to foreign keys).\n- Denormalization vs. Normalization: Trade-offs between embedding (denormalization) and referencing (normalization).\n\n### Schema Design Strategies:\n\n1. Embedding:-\n\nFor example:-\n\n```\n{\n  _id: ObjectId(\"64a12345c789ef0123456789\"),\n  title: \"Understanding MongoDB Data Modeling\",\n  content: \"Data modeling is crucial...\",\n  comments: [\n    { user: \"John\", comment: \"Great article!\", date: new Date() },\n    { user: \"Jane\", comment: \"Very helpful, thanks!\", date: new Date() }\n  ]\n}\n```\n\n2. Referencing:-\n\n```\n// User document\n{\n  _id: ObjectId(\"64a12345c789ef0123450000\"),\n  username: \"JohnDoe\",\n  email: \"john.doe@example.com\"\n}\n\n// Blog post document\n{\n  _id: ObjectId(\"64a12345c789ef0123456789\"),\n  title: \"Understanding MongoDB Data Modeling\",\n  content: \"Data modeling is crucial...\",\n  authorId: ObjectId(\"64a12345c789ef0123450000\")\n}\n```\n\n### Schema Validation in MongoDB\n\n#### Schema validation ensures that documents inserted or updated in a collection adhere to a specific structure. This helps maintain data consistency, even in a flexible schema database like MongoDB. MongoDB uses JSON Schema to enforce rules. This can be set during collection creation or updated.\n\nExample Schema Validation:\n\n```\ndb.createCollection(\"users\", {\n  validator: {\n    $jsonSchema: {\n      bsonType: \"object\",\n      required: [\"username\", \"email\"],\n      properties: {\n        username: {\n          bsonType: \"string\",\n          description: \"must be a string and is required\"\n        },\n        email: {\n          bsonType: \"string\",\n          pattern: \"^.+@.+\\\\..+$\",\n          description: \"must be a valid email and is required\"\n        },\n        age: {\n          bsonType: \"int\",\n          minimum: 18,\n          description: \"must be an integer and at least 18\"\n        }\n      }\n    }\n  }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuhammadfarooq85%2Fdata-modeling-schema-validation-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuhammadfarooq85%2Fdata-modeling-schema-validation-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuhammadfarooq85%2Fdata-modeling-schema-validation-mongodb/lists"}