{"id":6266,"date":"2025-05-31T14:34:50","date_gmt":"2025-06-04T06:37:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6266"},"modified":"2025-06-04T14:34:50","modified_gmt":"2025-06-04T06:37:50","slug":"%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8node-js%e8%88%87mongodb%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8node-js%e8%88%87mongodb%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96\/","title":{"rendered":"\u30102025 \u6700\u65b0\u7248\u3011\u4f7f\u7528 Python \u8207 MongoDB \u9032\u884c\u6578\u64da\u6301\u4e45\u5316\u7684\u5b8c\u6574\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, MongoDB, \u6578\u64da\u6301\u4e45\u5316, \u6559\u5b78, \u5be6\u4f5c\u7bc4\u4f8b\"><\/p>\n<h1>Python \u4e2d\u5982\u4f55\u4f7f\u7528 MongoDB \u9032\u884c\u6578\u64da\u6301\u4e45\u5316\uff1f<\/h1>\n<p>Python \u662f\u4e00\u500b\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u9818\u57df\uff0c\u5305\u62ec Web \u958b\u767c\u548c\u6578\u64da\u79d1\u5b78\u3002MongoDB \u662f\u4e00\u500b\u958b\u6e90\u7684 NoSQL \u8cc7\u6599\u5eab\uff0c\u9069\u5408\u5132\u5b58\u548c\u7ba1\u7406\u5927\u91cf\u8907\u96dc\u7684\u8cc7\u6599\u3002\u5728 Python \u4e2d\u4f7f\u7528 MongoDB \u9032\u884c\u6578\u64da\u6301\u4e45\u5316\u662f\u4e00\u7a2e\u5e38\u898b\u4e14\u6709\u6548\u7684\u505a\u6cd5\uff0c\u80fd\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u8cc7\u6599\u3002<\/p>\n<h2>\u5728 Python \u4e2d\u4f7f\u7528 MongoDB \u7684\u6b65\u9a5f<\/h2>\n<p>\u8981\u5728 Python \u4e2d\u4f7f\u7528 MongoDB\uff0c\u958b\u767c\u8005\u9700\u8981\u5b89\u88dd MongoDB \u7684 Python \u9a45\u52d5\u7a0b\u5f0f `pymongo`\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 MongoDB \u7684\u6b65\u9a5f\uff1a<\/p>\n<p>1. **\u5b89\u88dd pymongo \u9a45\u52d5\u7a0b\u5f0f**\uff1a<br \/>\n   \u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88dd pymongo \u9a45\u52d5\u7a0b\u5f0f\uff0c\u9019\u4f7f\u5f97 Python \u53ef\u4ee5\u8207 MongoDB \u9032\u884c\u4e92\u52d5\u3002<br \/>\n   &#8220;`bash<br \/>\n   pip install pymongo<br \/>\n   &#8220;`<\/p>\n<p>2. **\u9023\u63a5 MongoDB**\uff1a<br \/>\n   \u4f7f\u7528 pymongo \u9a45\u52d5\u7a0b\u5f0f\u4f86\u9023\u63a5 MongoDB\uff0c\u4e26\u50b3\u905e\u8cc7\u6599\u5eab\u7684 URL\u3002<br \/>\n   &#8220;`python<br \/>\n   from pymongo import MongoClient<\/p>\n<p>   client = MongoClient(&#8216;mongodb:\/\/localhost:27017\/&#8217;)<br \/>\n   db = client[&#8216;mydb&#8217;]\n   &#8220;`<\/p>\n<p>3. **\u5efa\u7acb\u96c6\u5408**\uff1a<br \/>\n   \u5728\u8cc7\u6599\u5eab\u4e2d\u5efa\u7acb\u96c6\u5408\uff0c\u9019\u76f8\u7576\u65bc SQL \u4e2d\u7684\u8cc7\u6599\u8868\u3002<br \/>\n   &#8220;`python<br \/>\n   collection = db[&#8216;users&#8217;]\n   &#8220;`<\/p>\n<p>4. **\u5132\u5b58\u8cc7\u6599**\uff1a<br \/>\n   \u4f7f\u7528 `.insert_one()` \u6216 `.insert_many()` \u65b9\u6cd5\u4f86\u5132\u5b58\u8cc7\u6599\u3002<br \/>\n   &#8220;`python<br \/>\n   user = {&#8220;name&#8221;: &#8220;John&#8221;, &#8220;age&#8221;: 30}<br \/>\n   collection.insert_one(user)<br \/>\n   &#8220;`<\/p>\n<p>5. **\u67e5\u8a62\u8cc7\u6599**\uff1a<br \/>\n   \u4f7f\u7528 `.find()` \u65b9\u6cd5\u4f86\u67e5\u8a62\u8cc7\u6599\u3002<br \/>\n   &#8220;`python<br \/>\n   for user in collection.find():<br \/>\n       print(user)<br \/>\n   &#8220;`<\/p>\n<p>6. **\u66f4\u65b0\u8cc7\u6599**\uff1a<br \/>\n   \u4f7f\u7528 `.update_one()` \u6216 `.update_many()` \u65b9\u6cd5\u4f86\u66f4\u65b0\u8cc7\u6599\u3002<br \/>\n   &#8220;`python<br \/>\n   collection.update_one({&#8220;name&#8221;: &#8220;John&#8221;}, {&#8220;$set&#8221;: {&#8220;age&#8221;: 31}})<br \/>\n   &#8220;`<\/p>\n<p>7. **\u522a\u9664\u8cc7\u6599**\uff1a<br \/>\n   \u4f7f\u7528 `.delete_one()` \u6216 `.delete_many()` \u65b9\u6cd5\u4f86\u522a\u9664\u8cc7\u6599\u3002<br \/>\n   &#8220;`python<br \/>\n   collection.delete_one({&#8220;name&#8221;: &#8220;John&#8221;})<br \/>\n   &#8220;`<\/p>\n<h2>\u5b8c\u6574\u7a0b\u5f0f\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684 Python \u7a0b\u5f0f\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 MongoDB \u9032\u884c\u6578\u64da\u6301\u4e45\u5316\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom pymongo import MongoClient<\/p>\n<p># \u9023\u63a5 MongoDB<br \/>\nclient = MongoClient(&#8216;mongodb:\/\/localhost:27017\/&#8217;)<br \/>\ndb = client[&#8216;mydb&#8217;]\ncollection = db[&#8216;users&#8217;]\n<p># \u5132\u5b58\u8cc7\u6599<br \/>\nuser = {&#8220;name&#8221;: &#8220;John&#8221;, &#8220;age&#8221;: 30}<br \/>\ncollection.insert_one(user)<\/p>\n<p># \u67e5\u8a62\u8cc7\u6599<br \/>\nfor user in collection.find():<br \/>\n    print(user)<\/p>\n<p># \u66f4\u65b0\u8cc7\u6599<br \/>\ncollection.update_one({&#8220;name&#8221;: &#8220;John&#8221;}, {&#8220;$set&#8221;: {&#8220;age&#8221;: 31}})<\/p>\n<p># \u522a\u9664\u8cc7\u6599<br \/>\ncollection.delete_one({&#8220;name&#8221;: &#8220;John&#8221;})<\/p>\n<p># \u95dc\u9589\u9023\u63a5<br \/>\nclient.close()<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u9996\u5148\u9023\u63a5\u5230 MongoDB\uff0c\u63a5\u8457\u5132\u5b58\u4e00\u500b\u4f7f\u7528\u8005\u8cc7\u6599\uff0c\u7136\u5f8c\u67e5\u8a62\u3001\u66f4\u65b0\u548c\u522a\u9664\u8a72\u8cc7\u6599\uff0c\u6700\u5f8c\u95dc\u9589\u9023\u63a5\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u4f7f\u7528 MongoDB \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **\u7121\u6cd5\u9023\u63a5\u5230 MongoDB**\uff1a\u6aa2\u67e5 MongoDB \u662f\u5426\u6b63\u5728\u904b\u884c\uff0c\u4e26\u78ba\u4fdd\u4f7f\u7528\u6b63\u78ba\u7684\u9023\u63a5\u5b57\u7b26\u4e32\u3002<br \/>\n&#8211; **\u8cc7\u6599\u672a\u5132\u5b58**\uff1a\u6aa2\u67e5\u662f\u5426\u6b63\u78ba\u4f7f\u7528\u4e86 `.insert_one()` \u65b9\u6cd5\uff0c\u4e26\u78ba\u4fdd\u8cc7\u6599\u683c\u5f0f\u6b63\u78ba\u3002<br \/>\n&#8211; **\u67e5\u8a62\u7121\u7d50\u679c**\uff1a\u78ba\u4fdd\u67e5\u8a62\u689d\u4ef6\u6b63\u78ba\uff0c\u4e26\u4e14\u8cc7\u6599\u5eab\u4e2d\u6709\u76f8\u61c9\u7684\u8cc7\u6599\u3002<\/p>\n<p>\u5ef6\u4f38\u61c9\u7528\u65b9\u9762\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9032\u4e00\u6b65\u5b78\u7fd2\u5982\u4f55\u5c07 Flask \u6216 Django \u8207 MongoDB \u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u4fbf\u958b\u767c\u529f\u80fd\u66f4\u5b8c\u6574\u7684 Web \u61c9\u7528\u3002<\/p>\n<p>\u5728\u4f7f\u7528 MongoDB \u9032\u884c\u6578\u64da\u6301\u4e45\u5316\u7684\u904e\u7a0b\u4e2d\uff0c\u82e5\u4f60\u60f3\u7372\u5f97\u66f4\u591a\u8cc7\u6e90\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/5e8e5c1cfd897800068b4b5f) \u4f86\u6df1\u5165\u4e86\u89e3 Python \u8207 MongoDB \u7684\u6574\u5408\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u6211\u9700\u8981\u5b89\u88dd\u4ec0\u9ebc\u624d\u80fd\u5728 Python \u4e2d\u4f7f\u7528 MongoDB\uff1f**<br \/>\nA1: \u4f60\u9700\u8981\u5b89\u88dd `pymongo` \u9a45\u52d5\u7a0b\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4 `pip install pymongo` \u4f86\u5b89\u88dd\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 MongoDB \u7684\u9023\u63a5\u554f\u984c\uff1f**<br \/>\nA2: \u78ba\u4fdd MongoDB \u670d\u52d9\u6b63\u5728\u904b\u884c\uff0c\u4e26\u6aa2\u67e5\u9023\u63a5\u5b57\u7b26\u4e32\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>**Q3: Python \u4e2d\u5982\u4f55\u67e5\u8a62 MongoDB \u4e2d\u7684\u8cc7\u6599\uff1f**<br \/>\nA3: \u4f60\u53ef\u4ee5\u4f7f\u7528 `.find()` \u65b9\u6cd5\u4f86\u67e5\u8a62\u8cc7\u6599\uff0c\u4e26\u53ef\u4ee5\u50b3\u905e\u689d\u4ef6\u4f86\u9032\u884c\u904e\u6ffe\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Node.js\u8207MongoDB\u9032\u884c\u6578\u64da\u6301\u4e45\u5316\uff0c\u8a73\u7d30\u89e3\u91cb\u4e86Node.js\u8207MongoDB\u4e4b\u9593\u7684\u4e92\u52d5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Node.js\u5c0dMongoDB\u9032\u884c\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5982\u4f55\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230MongoDB\u4e2d\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[193,192],"tags":[191,190],"class_list":["post-6266","post","type-post","status-publish","format-standard","hentry","category-node","category-node-js","tag-node","tag-node-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1D4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6266"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6266\/revisions"}],"predecessor-version":[{"id":6267,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6266\/revisions\/6267"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}