{"id":20615034,"url":"https://github.com/farooq85-dev/conditions-variables-in-mongodb","last_synced_at":"2025-03-06T18:44:24.794Z","repository":{"id":263096147,"uuid":"886812930","full_name":"Farooq85-dev/Conditions-Variables-In-MongoDB","owner":"Farooq85-dev","description":"This repository is about how can we use conditions and variables in MongoDb.","archived":false,"fork":false,"pushed_at":"2024-11-16T05:59:18.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T04:15:27.560Z","etag":null,"topics":["database","mongodb","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/Farooq85-dev.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-11T16:45:08.000Z","updated_at":"2024-11-16T05:59:22.000Z","dependencies_parsed_at":"2024-11-16T06:26:39.996Z","dependency_job_id":"6b92e486-60ed-41ae-80c1-61bc4114f3d4","html_url":"https://github.com/Farooq85-dev/Conditions-Variables-In-MongoDB","commit_stats":null,"previous_names":["farooq85-dev/conditions-variables-in-mongodb"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Farooq85-dev%2FConditions-Variables-In-MongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Farooq85-dev%2FConditions-Variables-In-MongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Farooq85-dev%2FConditions-Variables-In-MongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Farooq85-dev%2FConditions-Variables-In-MongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Farooq85-dev","download_url":"https://codeload.github.com/Farooq85-dev/Conditions-Variables-In-MongoDB/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242269117,"owners_count":20100071,"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","no-sql"],"created_at":"2024-11-16T11:14:13.249Z","updated_at":"2025-03-06T18:44:24.761Z","avatar_url":"https://github.com/Farooq85-dev.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Conditions-Variables-In-MongoDB\n\n### Why conditions in MongoDb?\n\n#### MongoDB uses a JSON-like syntax for conditions, primarily used in the find method to filter documents based on criteria. Conditions allow you to query documents that meet specific requirements. They are essential for finding, updating, or deleting data selectively.\n\n### 1) `gt`\n\n#### Find documents where the \"age\" field is greater than 30\n\n```\ndb.users.find({ age: { $gt: 30 } });\n```\n\n### 2) `find`\n\n#### Find documents where \"name\" is \"John\" and \"age\" is 25\n\n```\ndb.users.find({ name: \"John\", age: 25 });\n```\n\n### 3) `in`\n\n#### Find documents where \"status\" is either \"active\" or \"pending\"\n\n```\ndb.users.find({ status: { $in: [\"active\", \"pending\"] } });\n```\n\n### 4) `in`\n\n#### Matches documents where the field is equal to a specified value.\n\n```\ndb.users.find({ age: { $eq: 25 } });\n```\n\n### 5) `cond`\n\n#### The $cond operator is similar to an if-else statement, allowing you to add conditions for transformations and computations.\n\n```\ndb.sales.aggregate([\n  {\n    $project: {\n      status: {\n        $cond: {\n          if: { $gte: [\"$total\", 100] },\n          then: \"High\",\n          else: \"Low\"\n        }\n      }\n    }\n  }\n]);\n```\n\n### 6) `cond`\n\n#### In MongoDB, the $switch statement is used within aggregation pipelines to evaluate a series of conditions and return a result based on the first condition that evaluates to true. It functions similarly to a switch or case statement found in other programming languages. The $switch operator is ideal for handling multiple conditional checks in a clean and readable way. When you have more than two conditions to evaluate, $switch can simplify the logic compared to using multiple $cond statements.\n\n```\ndb.products.aggregate([\n  {\n    $project: {\n      name: 1,\n      priceCategory: {\n        $switch: {\n          branches: [\n            { case: { $lt: [\"$price\", 50] }, then: \"Low\" },\n            { case: { $and: [{ $gte: [\"$price\", 50] }, { $lt: [\"$price\", 200] }] }, then: \"Medium\" },\n            { case: { $gte: [\"$price\", 200] }, then: \"High\" }\n          ],\n          default: \"Unknown\"\n        }\n      }\n    }\n  }\n]);\n```\n\n### Why varables?\n\n#### Variables in MongoDB are used within aggregation pipelines to store intermediate values or handle data transformations. Variables enable complex data manipulations and calculations inside an aggregation pipeline, making it easier to pass values between stages.\n\n### 1) `let`\n\n#### Using $let to define a variable:\n\n```\ndb.sales.aggregate([\n  {\n    $project: {\n      totalRevenue: {\n        $let: {\n          vars: {\n            unitPrice: \"$price\",\n            unitsSold: \"$quantity\"\n          },\n          in: { $multiply: [\"$$unitPrice\", \"$$unitsSold\"] }\n        }\n      }\n    }\n  }\n]);\n```\n\n### 2) `NOW`\n\n#### This system variable is used to find current date of our system.\n\n```\ndb.collection.aggregate({$eq:[\"$price\",\"100\"]})\n```\n\n### 3) `System Variables`\n\n```\n$$NOW: Represents the current date and time. Useful for comparing date fields to the current time.\n$$ROOT: References the entire document at the current stage of the pipeline.\n$$CURRENT: Refers to the current document being processed in the pipeline stage. Similar to $$ROOT, but differs when used in nested stages.\n$$REMOVE: Used to indicate that a field should be removed from a document.\n$$DESCEND: Applies to expressions that traverse arrays, allowing you to process nested array fields.\n```\n\n### 4) `User Variables`\n\n- User variable names can contain the ascii characters [_a-zA-Z0-9] and any non-ascii character.\n- User variable names must begin with a lowercase ascii letter [a-z] or a non-ascii character.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarooq85-dev%2Fconditions-variables-in-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarooq85-dev%2Fconditions-variables-in-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarooq85-dev%2Fconditions-variables-in-mongodb/lists"}