{"id":18160225,"url":"https://github.com/swimshahriar/mongodb-cheat-sheet-plus","last_synced_at":"2026-04-27T22:31:14.883Z","repository":{"id":175339382,"uuid":"653734262","full_name":"swimshahriar/mongodb-cheat-sheet-plus","owner":"swimshahriar","description":"MongoDB Cheat Sheet Plus.","archived":false,"fork":false,"pushed_at":"2023-06-17T05:00:09.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T03:51:10.824Z","etag":null,"topics":["documentation","mongodb"],"latest_commit_sha":null,"homepage":"","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/swimshahriar.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":"2023-06-14T16:06:37.000Z","updated_at":"2023-06-25T18:30:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"a2b89a0f-9de0-4ee2-8d7b-1f41c568247f","html_url":"https://github.com/swimshahriar/mongodb-cheat-sheet-plus","commit_stats":null,"previous_names":["swimshahriar/mongodb-cheat-sheet"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/swimshahriar/mongodb-cheat-sheet-plus","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimshahriar%2Fmongodb-cheat-sheet-plus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimshahriar%2Fmongodb-cheat-sheet-plus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimshahriar%2Fmongodb-cheat-sheet-plus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimshahriar%2Fmongodb-cheat-sheet-plus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swimshahriar","download_url":"https://codeload.github.com/swimshahriar/mongodb-cheat-sheet-plus/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swimshahriar%2Fmongodb-cheat-sheet-plus/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32358509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"ssl_error","status_checked_at":"2026-04-27T20:07:00.910Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["documentation","mongodb"],"created_at":"2024-11-02T08:07:46.552Z","updated_at":"2026-04-27T22:31:14.855Z","avatar_url":"https://github.com/swimshahriar.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MongoDB Cheat Sheet\n\n## What is MongoDB?\n\nMongoDB is a popular, NoSQL (non-relational) database management system (DBMS). MongoDB uses JSON-like documents with optional schemas.\n\n## When to use MongoDB?\n\n- Use MongoDB when you need a flexible and schema-less database.\n- Choose MongoDB for applications with large amounts of unstructured or semi-structured data.\n- MongoDB is suitable for real-time data processing and analytics.\n- It works well for high-read workloads and scaling horizontally.\n- A better fit for applications that require low-latency data.\n- A better choice for distributed applications, where data needs to be spread across multiple servers.\n\n## Terminology\n\n- **Collection**: A grouping of documents inside of a database. This is the same as a table in SQL and usually, each type of data (users, posts, products) will have its own collection.\n\n- **Document**:\n  A record inside of a collection. This is the same as a row in SQL and usually, there will be one document per object in the collection. A document is also a JSON object.\n\n- **Field**: A key-value pair within a document. This is the same as a column in SQL. Each document will have some number of fields that contain information such as name, address, hobbies, etc. A significant difference between SQL and MongoDB is that a field can contain values such as JSON objects, and arrays instead of just strings, numbers, booleans, etc.\n\n# Installation\n\n## On MacOS\n\n```bash\nbrew tap mongodb/brew\nbrew update\nbrew install mongodb-community@6.0\n\n# for shell\nbrew install mongosh\n```\n\n## Toggle MongoDB Service\n\n```bash\nbrew services start mongodb-community@6.0\nbrew services stop mongodb-community@6.0\n```\n\n**Note**: For other OS please follow [these instructions](https://www.mongodb.com/docs/mongodb-shell/install/#std-label-mdb-shell-install).\n\n## Access Shell\n\n```bash\nmongosh\n```\n\n# Basic\n\n## Show All Databases\n\n```bash\nshow dbs\n```\n\n## Show Current Database\n\n```bash\ndb\n```\n\n## Create Or Switch Database\n\n```bash\nuse posts\n```\n\n## Drop\n\n```bash\ndb.dropDatabase()\n```\n\n## Create Collection\n\n```bash\ndb.createCollection('posts')\n```\n\n### Show Collections\n\n```bash\nshow collections\n```\n\n# Create\n\n## Insert Row\n\n```bash\ndb.posts.insertOne({\n  title: 'Post One',\n  body: 'Body of post one',\n  tags: ['news', 'events'],\n  reactions: 2,\n  views: 20,\n  userId: 1,\n})\n```\n\n## Insert Multiple Rows\n\n```bash\ndb.posts.insertMany([\n  {\n    title: 'Post Two',\n    body: 'Body of post two',\n    category: 'Technology',\n    tags: ['news', 'events'],\n    reactions: 20,\n    views: 30,\n    userId: 1,\n    date: Date()\n  },\n  {\n    title: 'Post Three',\n    body: 'Body of post three',\n    category: 'News',\n    tags: ['news', 'events'],\n    reactions: 12,\n    views: 10,\n    userId: 1,\n    date: Date()\n  },\n  {\n    title: 'Post Four',\n    body: 'Body of post three',\n    category: 'Entertainment',\n    tags: ['news', 'events'],\n    reactions: 10,\n    views: 3,\n    userId: 1,\n    date: Date()\n  }\n])\n```\n\n# Read\n\n## Get All Rows\n\n```bash\ndb.posts.find()\n```\n\n## Get All Rows Formatted\n\n```bash\ndb.posts.find().pretty()\n```\n\n## Find Rows\n\n```bash\ndb.posts.find({ userId: 1 })\n```\n\n## Find One Row\n\n```bash\ndb.posts.findOne({ id: 1 })\n```\n\n## Find Specific Fields\n\n```bash\ndb.posts.find({ title: 'Post One' }, {\n  title: 1,\n  userId: 1\n})\n```\n\n## Count Rows\n\n```bash\ndb.posts.countDocuments()\n```\n\n# Read Modifiers\n\n## Sort Rows\n\n```bash\n# asc\ndb.posts.find().sort({ title: 1 }).pretty()\n# desc\ndb.posts.find().sort({ title: -1 }).pretty()\n```\n\n## Limit Rows\n\n```bash\ndb.posts.find().limit(2).pretty()\n```\n\n## Chaining\n\n```bash\ndb.posts.find().limit(2).sort({ title: 1 }).pretty()\n```\n\n## Greater \u0026 Less Than\n\n```bash\ndb.posts.find({ views: { $gt: 2 } })\ndb.posts.find({ views: { $gte: 7 } })\ndb.posts.find({ views: { $lt: 7 } })\ndb.posts.find({ views: { $lte: 7 } })\n```\n\n## Complex Filter\n\n```bash\ndb.posts.find({ name: { $eq: \"shahriar\" } })\ndb.posts.find({ name: { $ne: \"shahriar\" } })\ndb.posts.find({ name: { $in: [\"shahriar\", \"swim\"] } })\ndb.posts.find({ name: { $nin: [\"shahriar\", \"swim\"] } })\ndb.posts.find({$or: [{name: \"shahriar\"}, {views: 3}]})\ndb.posts.find({name: {$not: {$eq: \"shahriar\"}}})\ndb.posts.find({name: {$exists: true}})\ndb.posts.find({ $expr: { $gt: [\"$views\", \"$reactions\"] } })\n```\n\n# Update\n\n## Update Row\n\n```bash\ndb.posts.updateOne({ title: 'Post Two' },\n{\n  title: 'Post Two',\n  body: 'New body for post 2',\n  date: Date()\n})\n```\n\n## Update Specific Field\n\n```bash\ndb.posts.updateOne({ title: 'Post Two' },\n{\n  $set: {\n    body: 'Body for post 2',\n  }\n})\n```\n\n## Update Many Field\n\n```bash\ndb.posts.updateMany({ userId: 1 },\n{\n  $set: {\n    userId: 2\n  }\n})\n```\n\n## Increment Field ($inc)\n\n```bash\ndb.posts.updateOne({ title: 'Post Two' },\n{\n  $inc: {\n    reactions: 5\n  }\n})\n```\n\n## Rename Field\n\n```bash\ndb.posts.updateOne({ title: 'Post Two' },\n{\n  $rename: {\n    reactions: 'favorites'\n  }\n})\n```\n\n## Remove a Field\n\n```bash\ndb.posts.replaceOne({ title: 'Post Two' },\n{\n  $unset: {title: \"\"}\n})\n```\n\n## Push into a Field\n\n```bash\ndb.posts.updateOne({ title: 'Post Two' },\n{\n  $push: {tags: \"new tag\"}\n})\n```\n\n## Pull from a Field\n\n```bash\ndb.posts.updateOne({ title: 'Post Two' },\n{\n  $pull: {tags: \"new tag\"}\n})\n```\n\n# Delete\n\n## Delete Row\n\n```bash\ndb.posts.deleteOne({ title: 'Post Four' })\n```\n\n## Delete Multiple Row\n\n```bash\ndb.posts.deleteMany({ userId: 1 })\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswimshahriar%2Fmongodb-cheat-sheet-plus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswimshahriar%2Fmongodb-cheat-sheet-plus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswimshahriar%2Fmongodb-cheat-sheet-plus/lists"}