{"id":168115,"date":"2024-11-06T09:38:23","date_gmt":"2024-11-06T01:38:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168115\/"},"modified":"2024-11-06T09:38:24","modified_gmt":"2024-11-06T01:38:24","slug":"%e9%9b%b6%e5%9f%ba%e7%a4%8e%e5%85%a5%e9%96%80python%e8%88%87mongodb%ef%bc%9a%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168115\/","title":{"rendered":"\u96f6\u57fa\u790e\u5165\u9580Python\u8207MongoDB\uff1a\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u7ba1\u7406"},"content":{"rendered":"<h1 id=\"%e9%9b%b6%e5%9f%ba%e7%a4%8e%e5%85%a5%e9%96%80python%e8%88%87mongodb%ef%bc%9a%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e7%ae%a1%e7%90%86-yhzURNtGkU\">\u96f6\u57fa\u790e\u5165\u9580Python\u8207MongoDB\uff1a\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u7ba1\u7406<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0c\u5b78\u7fd2\u5982\u4f55\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u7c21\u55ae\u6613\u5b78\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u8207MongoDB\u9019\u7a2e\u9748\u6d3b\u7684NoSQL\u6578\u64da\u5eab\u7d50\u5408\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u7ba1\u7406\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5f9e\u96f6\u57fa\u790e\u958b\u59cb\uff0c\u4f7f\u7528Python\u8207MongoDB\u9032\u884c\u6578\u64da\u7ba1\u7406\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87python%e5%92%8cmongodb%ef%bc%9f-yhzURNtGkU\">\u70ba\u4ec0\u9ebc\u9078\u64c7Python\u548cMongoDB\uff1f<\/h2>\n<p>Python\u662f\u4e00\u7a2e\u9ad8\u7d1a\u7de8\u7a0b\u8a9e\u8a00\uff0c\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u7121\u8ad6\u662f\u6578\u64da\u5206\u6790\u3001\u7db2\u9801\u958b\u767c\u9084\u662f\u81ea\u52d5\u5316\u4efb\u52d9\uff0cPython\u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u3002\u800cMongoDB\u4f5c\u70ba\u4e00\u7a2e\u6587\u6a94\u5c0e\u5411\u7684NoSQL\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\uff0c\u9069\u5408\u5b58\u5132\u7d50\u69cb\u5316\u548c\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e8%a8%ad%e7%bd%ae-yhzURNtGkU\">\u74b0\u5883\u8a2d\u7f6e<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5b89\u88ddPython\u548cMongoDB\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u8a2a\u554f<a href=\"https:\/\/www.python.org\/downloads\/\" rel=\"nofollow noopener\" target=\"_blank\">Python\u5b98\u65b9\u7db2\u7ad9<\/a>\u4e0b\u8f09\u4e26\u5b89\u88dd\u6700\u65b0\u7248\u672c\u7684Python\u3002<\/li>\n<li>\u8a2a\u554f<a href=\"https:\/\/www.mongodb.com\/try\/download\/community\" rel=\"nofollow noopener\" target=\"_blank\">MongoDB\u5b98\u65b9\u7db2\u7ad9<\/a>\u4e0b\u8f09\u4e26\u5b89\u88ddMongoDB\u3002<\/li>\n<li>\u5b89\u88ddPython\u7684MongoDB\u9a45\u52d5\u7a0b\u5e8f\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ul>\n<pre><code>pip install pymongo<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0mongodb-yhzURNtGkU\">\u9023\u63a5\u5230MongoDB<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230MongoDB\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>from pymongo import MongoClient\n\n# \u9023\u63a5\u5230MongoDB\nclient = MongoClient('localhost', 27017)\n\n# \u5275\u5efa\u6216\u9078\u64c7\u6578\u64da\u5eab\ndb = client['mydatabase']\n<\/code><\/pre>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e7%9a%84%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-yhzURNtGkU\">\u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c<\/h2>\n<p>\u5728MongoDB\u4e2d\uff0c\u6578\u64da\u4ee5\u6587\u6a94\u7684\u5f62\u5f0f\u5b58\u5132\u5728\u96c6\u5408\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-yhzURNtGkU\">\u63d2\u5165\u6578\u64da<\/h3>\n<pre><code># \u63d2\u5165\u55ae\u500b\u6587\u6a94\ndb.mycollection.insert_one({\"name\": \"Alice\", \"age\": 25})\n\n# \u63d2\u5165\u591a\u500b\u6587\u6a94\ndb.mycollection.insert_many([\n    {\"name\": \"Bob\", \"age\": 30},\n    {\"name\": \"Charlie\", \"age\": 35}\n])\n<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-yhzURNtGkU\">\u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code># \u67e5\u8a62\u6240\u6709\u6587\u6a94\nfor doc in db.mycollection.find():\n    print(doc)\n\n# \u67e5\u8a62\u7279\u5b9a\u689d\u4ef6\u7684\u6587\u6a94\nfor doc in db.mycollection.find({\"age\": {\"$gt\": 28}}):\n    print(doc)\n<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-yhzURNtGkU\">\u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code># \u66f4\u65b0\u55ae\u500b\u6587\u6a94\ndb.mycollection.update_one({\"name\": \"Alice\"}, {\"$set\": {\"age\": 26}})\n\n# \u66f4\u65b0\u591a\u500b\u6587\u6a94\ndb.mycollection.update_many({\"age\": {\"$lt\": 30}}, {\"$set\": {\"status\": \"young\"}})\n<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-yhzURNtGkU\">\u522a\u9664\u6578\u64da<\/h3>\n<pre><code># \u522a\u9664\u55ae\u500b\u6587\u6a94\ndb.mycollection.delete_one({\"name\": \"Bob\"})\n\n# \u522a\u9664\u591a\u500b\u6587\u6a94\ndb.mycollection.delete_many({\"age\": {\"$lt\": 30}})\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-yhzURNtGkU\">\u7d50\u8ad6<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u7684\u4ecb\u7d39\uff0c\u60a8\u61c9\u8a72\u5c0d\u5982\u4f55\u4f7f\u7528Python\u8207MongoDB\u9032\u884c\u57fa\u672c\u7684\u6578\u64da\u7ba1\u7406\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\u3002\u9019\u7a2e\u7d44\u5408\u4e0d\u50c5\u80fd\u5920\u5e6b\u52a9\u60a8\u8f15\u9b06\u5730\u8655\u7406\u6578\u64da\uff0c\u9084\u80fd\u70ba\u672a\u4f86\u7684\u9805\u76ee\u6253\u4e0b\u5805\u5be6\u7684\u57fa\u790e\u3002\u96a8\u8457\u60a8\u5c0d\u9019\u4e9b\u6280\u8853\u7684\u6df1\u5165\u5b78\u7fd2\uff0c\u60a8\u5c07\u80fd\u5920\u958b\u767c\u51fa\u66f4\u70ba\u8907\u96dc\u548c\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u7cfb\u7d71\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684<a href=\"https:\/\/server.hk\">VPS<\/a>\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u96f6\u57fa\u790ePython\u8207MongoDB\uff0c\u8f15\u9b06\u638c\u63e1\u6578\u64da\u7ba1\u7406\u6280\u5de7\uff0c\u5feb\u901f\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u8207\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-168115","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=168115"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168115\/revisions"}],"predecessor-version":[{"id":168116,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168115\/revisions\/168116"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}