{"id":19398738,"url":"https://github.com/nafeen/mongo-commands","last_synced_at":"2026-06-22T09:31:13.995Z","repository":{"id":240412115,"uuid":"69097671","full_name":"nafeen/Mongo-Commands","owner":"nafeen","description":"MongoDB commands cheatsheet. Contains map-reduce, aggregate etc.","archived":false,"fork":false,"pushed_at":"2017-10-10T08:08:32.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-03T23:38:04.087Z","etag":null,"topics":["mongodb","nosql"],"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/nafeen.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":"2016-09-24T11:15:36.000Z","updated_at":"2017-04-27T10:16:20.000Z","dependencies_parsed_at":"2024-05-18T17:45:18.042Z","dependency_job_id":"afad2704-add2-4dcc-93d0-f25aa1c98973","html_url":"https://github.com/nafeen/Mongo-Commands","commit_stats":null,"previous_names":["nafeen/mongo-commands"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nafeen/Mongo-Commands","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nafeen%2FMongo-Commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nafeen%2FMongo-Commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nafeen%2FMongo-Commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nafeen%2FMongo-Commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nafeen","download_url":"https://codeload.github.com/nafeen/Mongo-Commands/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nafeen%2FMongo-Commands/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34643483,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"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":["mongodb","nosql"],"created_at":"2024-11-10T11:06:58.358Z","updated_at":"2026-06-22T09:31:13.965Z","avatar_url":"https://github.com/nafeen.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n========================================================================\n MongoDB Commands\n========================================================================\n\n### How to easily run multi-line queries using 'VIM'\n\n_type in Mongo shell_\n\n    EDITOR = 'vim'\n    edit query\n\n_inside VIM editor, copy-paste query/function_\n_type ':x' to save and exit VIM editor_\n\n    db.sampleCollection.aggregate(query)\n\n\n========================================================================\n\n### Find ALL records\n\n    db.collectionName.find({}).pretty()\n\n\n### Filter ALL records based on field value(s)\n\n    db.collectionName.find({}, {\n        fieldName1 : value1,\n        fieldName2 : value2\n    }).pretty()\n\n\n### Find ONE record\n\n    db.collectionName.findOne()\n\n\n### Find DISTINCT field values\n\n    db.sampleCollection.distinct(\"fieldName\")\n\n\n### Find DISTINCT nested field values\n\n    db.sampleCollection.distinct(\"fieldName.nestedField\")\n\n\n========================================================================\n\n### Find ALL records BETWEEN two dates\n\n    begin_date_range = \"2016-09-20T00:00:00Z\";\n    end_date_range = \"2016-09-21T00:00:00Z\";\n\n    db.collectionName.find(\n        { \n            \"fieldName\": \n            {\n        \"$gte\":begin_date_range,\n        \"$lt\":end_date_range\n      }\n    }).pretty();\n\n\n========================================================================\n\n### Count of documents between TWO dates using ObjectId\n\n    var objIdMin = ObjectId(Math.floor((new Date('2016/1/7'))/1000).toString(16) + \"0000000000000000\");\n    \n    var objIdMax = ObjectId(Math.floor((new Date('2016/8/25'))/1000).toString(16) + \"0000000000000000\");\n\n    db.collectionName.aggregate([\n        {\n            $match : {\n                \"_id\" : {\n                    $gt: objIdMin, \n                    $lt: objIdMax\n                }\n            }\n        },\n        {\n            $group : {\n                _id : null,\n                count: { $sum: 1 }\n            }\n        }\n    ])\n\n\n========================================================================\n\n### Find all fields in a collection\n\n_using map-reduce_\n\n    mr = db.runCommand({\n        \"mapreduce\" : \"collectionName\",\n        \"map\" : function() {\n            for (var key in this) { emit(key, null); }\n        },\n        \"reduce\" : function(key, stuff) { return null; },\n        \"out\": \"fieldName\" + \"_keys\"\n    });\n\n    db[mr.result].distinct(\"fieldName\");\n\n\n========================================================================\n\n### Count distinct of all occurances of a particular field\n\n    db.collectionName.aggregate([\n        {\n          $match: {\n              keywords: { $not: {$size: 0} }\n          }\n        },\n        { \n            $unwind: \"$fieldName\" \n        },\n        {\n            $group: {\n                _id: {$toLower: '$fieldName'},\n                count: { $sum: 1 }\n            }\n        },\n        {\n            $match: {\n                count: { $gte: 2 }\n            }\n        },\n        { $sort : { count : -1} },\n        { $limit : 100 }\n    ]);\n\n\n========================================================================\n\n### Finding aggregate of metrics after grouping a field \n\n_analogous to SQL GROUP BY_\n\n    db.collectionName.aggregate([{\n        $group: {\n            \"_id\" : \"$fieldToGroupUnder\",\n            aggregateField1: { \n                $sum: \"$fieldName1\"\n            },\n            aggregateField2: { \n                $sum: \"$fieldName2.subFieldName\"\n            },\n        count: { $sum: 1 }\n        }\n    }]).pretty();\n\n\n========================================================================\n\n### Sort + Group using aggregate\n\n    db.collectionName.aggregate(\n        { \n            $sort: {\n                \"fieldName\" : -1\n            } \n        },\n        { \n            $group: {\n                fieldName: {\n                    $first: \"value\"\n                }\n            } \n        }\n    );\n\n\n========================================================================\n\n### Join + Filter + Select\n\n    db.collectionName1.aggregate([\n        {\n            $match : {\n                \"fieldToFilter\" : \"filterValue\"         // filter\n            }\n        },\n        {\n            $lookup : {                                 // Left-outer Join\n                from : \"collectionName2\",               // joining collection\n                localField : \"fieldFromCollection1\",    // joining field\n                foreignField : \"fieldFromCollection2\",  // joining field\n                as : \"joinFieldLabel\"                   // label for result\n            }\n        },\n        {\n            $project : {                                // FILTER result set\n                \"_id\" : 0,\n                \"filterValue\" : 1,\n                \"fieldName1\" : 1,\n                \"fieldName2.subFieldName\" : 1\n            }\n        }\n    ])\n\n\n========================================================================\n\n### Projecting all data and some custom fields with same query\n\n    db.collectionName.aggregate([\n      { \n        $project: {\n            data : \"$$ROOT\",            // this prints out all fields\n            customField: {\n                $concat : [ \"$fieldName1\", \"-\", \"$fieldName2\" ]\n            } \n        } \n        }, \n      { \n        $match : { \n            fieldName2 : \"fieldValue\"\n        } \n      }\n    ]).pretty();\n\n\n========================================================================\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnafeen%2Fmongo-commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnafeen%2Fmongo-commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnafeen%2Fmongo-commands/lists"}