{"id":18933841,"url":"https://github.com/mchamoudadev/mongodb-crash-course","last_synced_at":"2025-09-18T13:09:34.657Z","repository":{"id":214538741,"uuid":"736764479","full_name":"mchamoudadev/mongodb-crash-course","owner":"mchamoudadev","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-28T20:11:19.000Z","size":4,"stargazers_count":7,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-31T06:44:19.264Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/mchamoudadev.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}},"created_at":"2023-12-28T20:09:20.000Z","updated_at":"2025-04-16T10:35:56.000Z","dependencies_parsed_at":"2023-12-28T21:54:50.940Z","dependency_job_id":"119bf338-9bae-48fa-9387-1e84dbe0824f","html_url":"https://github.com/mchamoudadev/mongodb-crash-course","commit_stats":null,"previous_names":["mchamoudadev/mongodb-crash-course"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mchamoudadev/mongodb-crash-course","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fmongodb-crash-course","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fmongodb-crash-course/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fmongodb-crash-course/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fmongodb-crash-course/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mchamoudadev","download_url":"https://codeload.github.com/mchamoudadev/mongodb-crash-course/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mchamoudadev%2Fmongodb-crash-course/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275772569,"owners_count":25525901,"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-09-18T02:00:09.552Z","response_time":77,"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-11-08T11:56:37.630Z","updated_at":"2025-09-18T13:09:34.612Z","avatar_url":"https://github.com/mchamoudadev.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"### Databases: Introduction and Types\n\n**What is a Database?**:\n\n- A database is an organized collection of data, typically stored electronically. It's designed to efficiently store, manage, and retrieve data.\n- Databases play a crucial role in modern software development, enabling applications to store and manage vast amounts of information.\n\n**Types of Databases**:\n\n1. **Relational Databases**:\n\n   - **Description**: Store data in structured tables with rows and columns.\n   - **Example**: MySQL, PostgreSQL, SQLite, Oracle.\n   - **Pros**:\n     - Well-established, proven technology.\n     - Support for ACID (Atomicity, Consistency, Isolation, Durability) transactions.\n     - \u003e **🔔 Good to Know: ACID Properties in Databases**\n        \u003e\n        \u003e - **Atomicity**: Ensures all operations in a transaction are completed successfully as a single unit. If any operation fails, the entire transaction is rolled back.\n        \u003e - **Consistency**: Guarantees that a transaction will bring the database from one valid state to another, maintaining all predefined rules.\n        \u003e - **Isolation**: Ensures that transactions are executed in isolation from each other, preventing transactions from seeing intermediate results.\n        \u003e - **Durability**: Once a transaction is committed, it is permanent, even in the event of a system failure. This ensures data integrity post-transaction.\n        \u003e\n        \u003e ACID properties are essential for systems that require reliable data transactions, such as financial and banking systems.\n\n   - **Cons**:\n     - Not always suitable for highly dynamic or unstructured data.\n     - Schema changes can be challenging.\n\n2. **NoSQL Databases**:\n   - **Description**: Databases that store data in formats other than traditional rows and columns.\n   - **Example**: MongoDB, Cassandra, Redis.\n   - **Pros**:\n     - Suitable for handling dynamic, unstructured, or semi-structured data.\n     - Flexible schema allows easy adaptation to changing data requirements.\n   - **Cons**:\n     - May not support ACID transactions as well as relational databases.\n     - Learning curve for those accustomed to relational databases.\n\n---\n\n### MongoDB: A NoSQL Database\n\n**Introduction to MongoDB**:\n\n- **Description**: MongoDB is a popular open-source NoSQL database.\n- **Key Feature**: It uses a flexible, document-based model to store data, where each record is a document containing fields and values.\n- **Use Cases**: Web applications, mobile apps, content management systems, IoT, and more.\n\n**Advantages of MongoDB**:\n\n1. **Flexibility**:\n\n   - Schema-less design allows you to store heterogeneous data in the same collection.\n   - Changes to data structure can be made without affecting existing records.\n\n2. **Scalability**:\n\n   - Horizontal scaling is easily achieved through sharding, distributing data across multiple servers.\n   - Suitable for applications that require handling large volumes of data.\n\n3. **Query Language**:\n\n   - MongoDB's query language is powerful and expressive.\n   - Supports a wide range of queries, including aggregation and text search.\n\n4. **Speed**:\n   - No complex joins, as data is often stored in a denormalized format.\n   - Well-suited for read-heavy workloads.\n\n**When to Choose MongoDB**:\n\n- Choose MongoDB when you need to handle dynamic, unstructured data, require quick iteration, and prefer flexibility over rigid schema requirements.\n- Consider it for projects where speed and scalability are important.\n\n**MongoDB vs. Other NoSQL Databases**:\n\n- MongoDB is just one of several NoSQL databases. Each has its strengths and weaknesses, so it's essential to choose the one that best fits your project's requirements.\n\n---\n\n### MongoDB: Fundamental Operations and Terminologies\n\n**1. Terminology**:\n\n- **Database**: A MongoDB instance can host multiple databases. Each database is a set of collections, similar to how traditional databases host tables.\n\n- **Collection**: Equivalent to a table in relational databases. It holds a set of documents.\n\n- **Document**: A single data entity with fields and values, similar to a row in relational databases. Documents are stored in JSON-like format called BSON.\n\n- **Field**: A name-value pair inside a document. Similar to a column in relational databases.\n\n---\n\n**2. Basic Database Operations**:\n\n- **Create a New Database**:\n\n  ```javascript\n  use myNewDatabase;\n  ```\n\n  **Description**: This command switches to the specified database. If the database doesn't exist, MongoDB will create it once you add data.\n\n- **List All Databases**:\n\n  ```javascript\n  show dbs;\n  ```\n\n  **Description**: Displays a list of all databases on the MongoDB server.\n\n- **Switch Between Databases**:\n\n  ```javascript\n  use anotherDatabase;\n  ```\n\n  **Description**: Switches to the specified database.\n\n- **Drop a Database**:\n  ```javascript\n  db.dropDatabase();\n  ```\n  **Description**: Deletes the current database.\n\n---\n\n**3. Collections Management**:\n\n- **Create a Collection**:\n  MongoDB creates collections automatically when you insert data. However, you can also create them explicitly:\n\n  ```javascript\n  db.createCollection(\"myCollection\");\n  ```\n\n  **Description**: Creates a new collection named \"myCollection\" in the current database.\n\n- **List All Collections**:\n\n  ```javascript\n  show collections;\n  ```\n\n  **Description**: Displays a list of all collections in the current database.\n\n- **Drop a Collection**:\n  ```javascript\n  db.myCollection.drop();\n  ```\n  **Description**: Deletes the specified collection and its data.\n\n---\n\n**4. Documents Management**:\n\n- **Insert a Document**:\n\n  ```javascript\n  db.myCollection.insertOne({ name: \"Alice\", age: 25 });\n  ```\n\n  **Description**: Inserts a single document into the specified collection.\n\n- **Find Documents**:\n\n  ```javascript\n  db.myCollection.find();\n  ```\n\n  **Description**: Retrieves all documents from the specified collection.\n\n- **Update a Document**:\n\n  ```javascript\n  db.myCollection.updateOne({ name: \"Alice\" }, { $set: { age: 26 } });\n  ```\n\n  **Description**: Updates the first document that matches the query.\n\n- **Delete a Document**:\n  ```javascript\n  db.myCollection.deleteOne({ name: \"Alice\" });\n  ```\n  **Description**: Deletes the first document that matches the query.\n\n---\n\n**5. Indexing**:\n\n- **Create an Index**:\n\n  ```javascript\n  db.myCollection.createIndex({ name: 1 });\n  ```\n\n  **Description**: Creates an ascending index on the \"name\" field of the specified collection. Indexing speeds up query performance.\n\n- **List All Indexes**:\n  ```javascript\n  db.myCollection.getIndexes();\n  ```\n  **Description**: Displays a list of all indexes on the specified collection.\n\n---\n\nAlright, let's move on to the practical aspects of MongoDB.\n\n### MongoDB: Practical Introduction\n\n**1. Installing MongoDB**:\n\nTo install MongoDB, you'd typically follow the instructions provided in the official MongoDB documentation based on your operating system. Here's a general outline:\n\n- **Windows**: Download the MongoDB installer and follow the installation instructions.\n- **Mac**: Use a package manager like Homebrew: `brew install mongodb`.\n- **Linux**: Use the package manager specific to your distribution (like `apt` for Ubuntu) to install MongoDB.\n\n**2. Starting MongoDB**:\n\n- Once installed, you can start MongoDB using the `mongod` command.\n- To interact with MongoDB, you can use the `mongo` shell.\n\n**3. Basic CRUD Operations in MongoDB**:\n\n- **Create**: Use the `insertOne()` or `insertMany()` method.\n\n  ```javascript\n  db.collectionName.insertOne({ name: \"John\", age: 30 });\n  ```\n\n- **Read**: Use the `find()` method.\n\n  ```javascript\n  db.collectionName.find({ name: \"John\" });\n  ```\n\n- **Update**: Use the `updateOne()` or `updateMany()` method.\n\n  ```javascript\n  db.collectionName.updateOne({ name: \"John\" }, { $set: { age: 31 } });\n  ```\n\n- **Delete**: Use the `deleteOne()` or `deleteMany()` method.\n  ```javascript\n  db.collectionName.deleteOne({ name: \"John\" });\n  ```\n\n**4. MongoDB Atlas**:\n\n- For those who prefer a cloud solution, MongoDB Atlas provides a fully managed database service. You can create a cluster, connect your application, and start using MongoDB without managing the underlying infrastructure.\n\n---\n\n### MongoDB: Descriptive Queries and Operations\n\n**1. Inserting Data**:\n\n- **Insert a Single Document**:\n\n  ```javascript\n  db.collectionName.insertOne({\n  \tname: \"Alice\",\n  \tage: 25,\n  \tprofession: \"Engineer\",\n  });\n  ```\n\n  **Description**: Inserts a single document into the collection with the specified fields: name, age, and profession.\n\n- **Insert Multiple Documents**:\n  ```javascript\n  db.collectionName.insertMany([\n  \t{ name: \"Bob\", age: 30, profession: \"Doctor\" },\n  \t{ name: \"Charlie\", age: 28, profession: \"Artist\" },\n  \t{ name: \"David\", age: 35, profession: \"Lawyer\" },\n  ]);\n  ```\n  **Description**: Inserts multiple documents into the collection in one operation.\n\n**2. Querying Data**:\n\n- **Find All Documents in a Collection**:\n\n  ```javascript\n  db.collectionName.find();\n  ```\n\n  **Description**: Retrieves all documents from the specified collection.\n\n- **Find Specific Documents**:\n\n  ```javascript\n  db.collectionName.find({ name: \"Bob\" });\n  ```\n\n  **Description**: Searches for and retrieves documents where the name is \"Bob\".\n\n- **Query with Conditions**:\n\n  ```javascript\n  db.collectionName.find({ age: { $gt: 30 } });\n  ```\n\n  **Description**: Retrieves documents where the age is greater than 30.\n\n- **Projection**:\n  ```javascript\n  db.collectionName.find({}, { name: 1, _id: 0 });\n  ```\n  **Description**: Retrieves only the `name` field for all documents in the collection, excluding the `_id` field.\n\n**3. Updating Data**:\n\n- **Update a Single Document**:\n\n  ```javascript\n  db.collectionName.updateOne({ name: \"Alice\" }, { $set: { age: 26 } });\n  ```\n\n  **Description**: Updates the age of the first document where the name is \"Alice\" to 26.\n\n- **Update Multiple Documents**:\n\n  ```javascript\n  db.collectionName.updateMany({}, { $inc: { age: 1 } });\n  ```\n\n  **Description**: Increases the age of all documents in the collection by 1.\n\n- **Upsert**:\n  ```javascript\n  db.collectionName.updateOne(\n  \t{ name: \"Eve\" },\n  \t{ $set: { age: 40, profession: \"Teacher\" } },\n  \t{ upsert: true }\n  );\n  ```\n  **Description**: Updates the document with the name \"Eve\" if it exists. If it doesn't, a new document with the specified fields is inserted.\n\n**4. Deleting Data**:\n\n- **Delete a Single Document**:\n\n  ```javascript\n  db.collectionName.deleteOne({ name: \"David\" });\n  ```\n\n  **Description**: Deletes the first document where the name is \"David\".\n\n- **Delete Multiple Documents**:\n  ```javascript\n  db.collectionName.deleteMany({ age: { $lt: 30 } });\n  ```\n  **Description**: Deletes all documents where the age is less than 30.\n\n**5. Advanced Queries**:\n\n- **Logical Operators**:\n\n  ```javascript\n  db.collectionName.find({ $or: [{ name: \"Alice\" }, { name: \"Bob\" }] });\n  ```\n\n  **Description**: Retrieves documents where the name is either \"Alice\" or \"Bob\".\n\n- **Regular Expressions**:\n\n  ```javascript\n  db.collectionName.find({ name: /^A/ });\n  ```\n\n  **Description**: Searches for documents where the name starts with the letter \"A\".\n\n- **Count Documents**:\n\n  ```javascript\n  db.collectionName.countDocuments({ age: { $gt: 30 } });\n  ```\n\n  **Description**: Counts the number of documents where the age is greater than 30.\n\n- **Sorting**:\n\n  ```javascript\n  db.collectionName.find().sort({ age: -1 });\n  ```\n\n  **Description**: Retrieves all documents, sorted by age in descending order.\n\n- **Limit and Skip**:\n  ```javascript\n  db.collectionName.find().limit(10).skip(20);\n  ```\n  **Description**: Retrieves documents with pagination, showing 10 documents starting from the 21st document.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmchamoudadev%2Fmongodb-crash-course","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmchamoudadev%2Fmongodb-crash-course","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmchamoudadev%2Fmongodb-crash-course/lists"}