{"id":28028839,"url":"https://github.com/m-taghizadeh/pymongodb","last_synced_at":"2026-05-01T19:32:30.166Z","repository":{"id":104572120,"uuid":"324243898","full_name":"M-Taghizadeh/PyMongoDB","owner":"M-Taghizadeh","description":"in this repo we learn about MongoDB and Python mongodb (PyMongoDB)","archived":false,"fork":false,"pushed_at":"2020-12-24T22:21:55.000Z","size":7,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-11T07:33:48.712Z","etag":null,"topics":["mongodb","mongodb-database","mongodb-tutorial","pymongo","python"],"latest_commit_sha":null,"homepage":"","language":"Python","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/M-Taghizadeh.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,"zenodo":null}},"created_at":"2020-12-24T22:09:55.000Z","updated_at":"2022-12-25T13:52:08.000Z","dependencies_parsed_at":"2023-03-13T14:54:20.896Z","dependency_job_id":null,"html_url":"https://github.com/M-Taghizadeh/PyMongoDB","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/M-Taghizadeh/PyMongoDB","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyMongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyMongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyMongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyMongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/M-Taghizadeh","download_url":"https://codeload.github.com/M-Taghizadeh/PyMongoDB/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/M-Taghizadeh%2FPyMongoDB/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32510710,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"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","mongodb-database","mongodb-tutorial","pymongo","python"],"created_at":"2025-05-11T07:31:51.343Z","updated_at":"2026-05-01T19:32:30.157Z","avatar_url":"https://github.com/M-Taghizadeh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Get started with Mongo DB\nMongoDB is a cross-platform document-oriented database program.\nClassified as a NoSQL database program, MongoDB uses JSON-like documents\n\n- [Installation](#Installation)\n- [Create DataBase](#Create-DataBase)\n- [Drop DataBase](#Drop-DataBase)\n- [Create Collection](#Create-Collection)\n- [Insert documents to collection](#Insert-documents-to-collection)\n- [Find documents of collection](#Find-documents-of-collection)\n- [Update documents](#Update-documents)\n- [Replace documents](#Replace-documents)\n- [Delete documents](#Delete-documents)\n- [Counts of document](#Counts-of-document)\n- [Sorting Document](#Sorting-Document)\n- [Pagination Limit and Skip commands](#Pagination:Limit-and-Skip-commands)\n- [$inc $set $rename $unset](#$inc-$set-$rename-$unset)\n- [$gt $lt $gte $lte](#$gt-$lt-$gte-$lte)\n- [BulkWrite](#bulkWrite)\n- [Search and Indexing in MongoDB](#Search-in-MongoDB(Indexing))\n- [Backup and Restore Database](#Backup-and-Restore-Database)\n\n### Installation\nDownload mongodb from here : https://www.mongodb.com/try/download/community\n\n\n### Create-DataBase\n```shell \n\u003e\u003e\u003e show dbs # show all databases\n\u003e\u003e\u003e use db_name # create and switch to db\n\u003e\u003e\u003e db.db_name.insert({\"name\": \"mohammad\"})\n```\n\n### Drop-DataBase\n```shell \n\u003e\u003e\u003e use db_name\n\u003e\u003e\u003e db.dropDatabase()\nresutl : { \"dropped\" : \"db_name\", \"ok\" : 1 }\n\u003e\u003e\u003e db.copyDatabase(\"old db name\",\"new db name\") # rename database\n```\n\n### Create-Collection\n**Collection** in mongoDB == **Table** in relational database : Mongo uses collections instead of tables for store data \u003cbr\u003e\n**Document**  in mongoDB == **Record** in relational database : The collection includes documents\n\n```shell \n\u003e\u003e\u003e db.createCollection(\"coll_name\")\n\u003e\u003e\u003e show collections\n\u003e\u003e\u003e db.coll_name.renameCollection(\"new_coll_name\")\n\u003e\u003e\u003e db.coll_name.drop()\n```\n\n### Insert-documents-to-collection\n```shell \n\u003e\u003e\u003e use University\n\u003e\u003e\u003e db.createCollection(\"Students\")\n\u003e\u003e\u003e db.Student.insertOne({\"student_id\":1, \"name\":\"Mohammad Taghizadeh\", \"field\":\"comuter\", \"entry_date\":1395})\n\u003e\u003e\u003e db.Student.insert([\n        {\"student_id\":1, \"name\":\"Mohammad Taghizadeh\", \"field\":\"comuter\", \"entry_date\":1395},\n        {\"student_id\":2, \"name\":\"Ali\", \"field\":\"art\", \"entry_date\":1399},\n        {\"student_id\":3, \"name\":\"Reza\", \"field\":\"comuter\", \"entry_date\":1390},\n    ])\n```\n\n### Find-documents-of-collection\n```shell \n\u003e\u003e\u003e db.Student.findOne({\"name\":\"Mohammad\"})\n\u003e\u003e\u003e db.Student.find() # find all of docs\n\u003e\u003e\u003e db.Student.find().pretty() \n\u003e\u003e\u003e db.Student.find({\"name\":\"Mohammad\"}).pretty() \n\n```\n\n### Update-documents\n```shell \n\u003e\u003e\u003e db.Student.update({\"_id\" : ObjectId(\"5fe490ad2b7f96f9a5f8249b\")}, \n    {\n        $set: {\n            \"name\": \"Ahmad\"\n        }\n    })\n\n\u003e\u003e\u003e db.Student.update({\"name\" : \"Mohammad\")}, {\n        $set: {\"name\": \"Ahmad\"}\n    })\n\n\u003e\u003e\u003e db.Student.updateMany({name : \"Mohammad\")}, {\n        $set: {\"create_date\": Date()}\n    })\n\n\u003e\u003e\u003e db.Student.updateMany({}, {\n        $set: {\"create_time\": Date()}\n    })\n```\n\n### Replace-documents \n```shell \n\u003e\u003e\u003e  db.Student.replaceOne({name: \"Mohammad Taghizadeh\"}, {fullname: \"MTaghizadeh\", age: 23})\n```\n\n### Delete-documents\n```shell \n\u003e\u003e\u003e db.Student.deleteOne({name: \"Mohammad\"})\n\u003e\u003e\u003e db.Student.deleteMany({name: \"Mohammad\"})\n```\n\n### Counts-of-document\n```shell \n\u003e\u003e\u003e db.Student.find().count()\n\u003e\u003e\u003e db.Student.find({name: \"Mohamamd\"}).count()\n```\n\n### Sorting-Document\n```shell   \n\u003e\u003e\u003e db.Student.find().sort({student_id:  1}).pretty() # 1 : acs\n\u003e\u003e\u003e db.Student.find().sort({student_id: -1}).pretty() # 2 : desc\n\u003e\u003e\u003e db.Student.find({field: \"computer\"}).sort({student_id: -1}).pretty() # 2 : desc\n```\n\n### Pagination:Limit-and-Skip-commands\n```shell \n\u003e\u003e\u003e db.Posts.find().sort({create_date: -1}).limit(3).pretty() # limit\n\u003e\u003e\u003e db.Posts.find().sort({create_date: -1}).skip(3).limit(3).pretty() # skip\n\u003e\u003e\u003e db.Posts.find().sort({create_date: -1}).skip(6).limit(3).pretty() # skip\n\u003e\u003e\u003e db.Posts.find().sort({create_date: -1}).skip(paginated_by * (page -1)).limit(paginated_by).pretty() # (for example: paginated_by : 9)\n```\n\n### $inc-$set-$rename-$unset\n```shell\n\u003e\u003e\u003e db.Posts.updateOne({title: \"post1\"}, {$inc: {likes: 1}}) # rather than get value and update database (2 connection), $inc: (1 connection) \n\u003e\u003e\u003e db.Posts.updateOne({title: \"post1\"}, {$inc: {likes: -1}}) # rather than get value and update database (2 connection), $inc: (1 connection) \n\u003e\u003e\u003e db.Posts.updateMany({}, {$set: {views: 0}}) # $set\n\u003e\u003e\u003e db.Posts.updateMany({}, {$rename: {views: \"dislikes\"}}) # $rename\n\u003e\u003e\u003e db.Posts.updateMany({}, {$unset: {views: \"\"}}) # $remove or $unset\n```\n\n### $gt-$lt-$gte-$lte\n```shell \n\u003e\u003e\u003e  db.products.find({price: {$gt: 1000}}).pretty() # greater than\n\u003e\u003e\u003e  db.products.find({price: {$lt: 1000}}).pretty() # less than\n\u003e\u003e\u003e  db.products.find({price: {$gte: 1000}}).pretty() # greater than equal\n\u003e\u003e\u003e  db.products.find({price: {$lte: 1000}}).pretty() # less than equal\n```\n \n### bulkWrite\n```shell \n\u003e\u003e\u003e db.products.bulkWrite(\n    [\n        {\n            insertOne: {\n                'document': {\n                    title: \"Phone\",\n                    price: 1000,\n                    likes: 0\n                }\n            }\n        },\n        {\n            insertOne: {\n                'document': {\n                    title: \"Laptop\",\n                    price: 890,\n                    likes: 0\n                }\n            }\n        },\n        {\n            updateMany: {\n                filter: {\n                    likes: {\n                        $gte: 100\n                    }\n                },\n                update: {\n                    $set: {\n                        popular: true\n                    }\n                }\n            }\n        },\n        {\n            delteOne: {\n                filter: {\n                    title: \"Phone\"\n                }\n            }\n        },\n    ]\n)\n```\n\n### Search-in-MongoDB(Indexing)\n```shell \n# 1) create index on field\n\u003e\u003e\u003e db.products.createIndex({title: \"text\"})\n# 2) search\n\u003e\u003e\u003e db.products.find({$text: {$search: \"Phone\"}}).pretty()\n```\n\n### Backup-and-Restore-Database\n```shell \n\u003e\u003e\u003e mongodump --db db_name  \n\u003e\u003e\u003e mongorestore --db db_name \u003cdb_address\u003e\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-taghizadeh%2Fpymongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm-taghizadeh%2Fpymongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-taghizadeh%2Fpymongodb/lists"}