{"id":247438,"date":"2024-01-02T22:20:25","date_gmt":"2024-01-02T14:20:25","guid":{"rendered":"https:\/\/www.idc.net\/help\/247438\/"},"modified":"2024-01-02T22:20:25","modified_gmt":"2024-01-02T14:20:25","slug":"%e5%a6%82%e4%bd%95%e4%bf%ae%e5%a4%8dmongodb%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81-16-%e9%95%bf%e5%ba%a6%e6%97%a0%e6%95%88","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/247438\/","title":{"rendered":"\u5982\u4f55\u4fee\u590dMongoDB\u9519\u8bef\u4ee3\u7801 &#8211; 16 &#8211; \u957f\u5ea6\u65e0\u6548"},"content":{"rendered":"<p><title id=\"%e5%a6%82%e4%bd%95%e4%bf%ae%e5%a4%8dmongodb%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81-16-%e9%95%bf%e5%ba%a6%e6%97%a0%e6%95%88-DYzwnuqpvL\">\u5982\u4f55\u4fee\u590dMongoDB\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548<\/title><\/p>\n<h1 id=\"%e5%a6%82%e4%bd%95%e4%bf%ae%e5%a4%8dmongodb%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81-16-%e9%95%bf%e5%ba%a6%e6%97%a0%e6%95%88-DYzwnuqpvL\">\u5982\u4f55\u4fee\u590dMongoDB\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548<\/h1>\n<p>MongoDB\u662f\u4e00\u79cd\u6d41\u884c\u7684\u5f00\u6e90\u6587\u6863\u6570\u636e\u5e93\uff0c\u88ab\u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u5e94\u7528\u7a0b\u5e8f\u548c\u7f51\u7ad9\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u5728\u4f7f\u7528MongoDB\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u8fd9\u4e2a\u9519\u8bef\u7684\u539f\u56e0\u4ee5\u53ca\u5982\u4f55\u4fee\u590d\u5b83\u3002<\/p>\n<h2 id=\"%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81-16-%e9%95%bf%e5%ba%a6%e6%97%a0%e6%95%88%e7%9a%84%e5%8e%9f%e5%9b%a0-DYzwnuqpvL\">\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\u7684\u539f\u56e0<\/h2>\n<p>\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\u901a\u5e38\u8868\u793a\u60a8\u6b63\u5728\u5c1d\u8bd5\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\uff0c\u800c\u8be5\u503c\u7684\u957f\u5ea6\u8d85\u8fc7\u4e86\u5b57\u6bb5\u7684\u6700\u5927\u957f\u5ea6\u9650\u5236\u3002\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u6570\u636e\u6a21\u578b\u8bbe\u8ba1\u4e0d\u5f53\u6216\u8f93\u5165\u6570\u636e\u7684\u9519\u8bef\u5bfc\u81f4\u7684\u3002<\/p>\n<h2 id=\"%e4%bf%ae%e5%a4%8d%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81-16-%e9%95%bf%e5%ba%a6%e6%97%a0%e6%95%88%e7%9a%84%e6%96%b9%e6%b3%95-DYzwnuqpvL\">\u4fee\u590d\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\u7684\u65b9\u6cd5<\/h2>\n<p>\u8981\u4fee\u590d\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\uff0c\u60a8\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<h3 id=\"1-%e6%a3%80%e6%9f%a5%e6%95%b0%e6%8d%ae%e6%a8%a1%e5%9e%8b-DYzwnuqpvL\">1. \u68c0\u67e5\u6570\u636e\u6a21\u578b<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u5e94\u8be5\u68c0\u67e5\u6570\u636e\u6a21\u578b\uff0c\u786e\u4fdd\u5b57\u6bb5\u7684\u6700\u5927\u957f\u5ea6\u9650\u5236\u4e0e\u60a8\u7684\u9700\u6c42\u76f8\u5339\u914d\u3002\u5982\u679c\u5b57\u6bb5\u7684\u6700\u5927\u957f\u5ea6\u9650\u5236\u592a\u5c0f\uff0c\u60a8\u53ef\u4ee5\u8003\u8651\u589e\u52a0\u5b83\u3002<\/p>\n<h3 id=\"2-%e9%aa%8c%e8%af%81%e8%be%93%e5%85%a5%e6%95%b0%e6%8d%ae-DYzwnuqpvL\">2. \u9a8c\u8bc1\u8f93\u5165\u6570\u636e<\/h3>\n<p>\u5728\u63d2\u5165\u6216\u66f4\u65b0\u6570\u636e\u4e4b\u524d\uff0c\u60a8\u5e94\u8be5\u9a8c\u8bc1\u8f93\u5165\u6570\u636e\u7684\u957f\u5ea6\u662f\u5426\u7b26\u5408\u5b57\u6bb5\u7684\u6700\u5927\u957f\u5ea6\u9650\u5236\u3002\u5982\u679c\u6570\u636e\u957f\u5ea6\u8d85\u8fc7\u4e86\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u622a\u65ad\u6570\u636e\u6216\u8005\u62d2\u7edd\u63d2\u5165\u6216\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e5%90%88%e9%80%82%e7%9a%84%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b-DYzwnuqpvL\">3. \u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b<\/h3>\n<p>\u786e\u4fdd\u60a8\u4f7f\u7528\u4e86\u9002\u5f53\u7684\u6570\u636e\u7c7b\u578b\u6765\u5b58\u50a8\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u5b58\u50a8\u8f83\u957f\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7c7b\u578b\u800c\u4e0d\u662f\u8f83\u77ed\u7684\u5b57\u7b26\u7c7b\u578b\u3002<\/p>\n<h3 id=\"4-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86-DYzwnuqpvL\">4. \u9519\u8bef\u5904\u7406<\/h3>\n<p>\u5728\u5904\u7406\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\u65f6\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u6355\u83b7\u8be5\u9519\u8bef\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u3002\u60a8\u53ef\u4ee5\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3001\u5411\u7528\u6237\u663e\u793a\u53cb\u597d\u7684\u9519\u8bef\u63d0\u793a\u6216\u8005\u56de\u6eda\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81-DYzwnuqpvL\">\u793a\u4f8b\u4ee3\u7801<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528pymongo\u5e93\u6765\u5904\u7406\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\uff1a<\/p>\n<pre><code>\nfrom pymongo import MongoClient\n\n# \u8fde\u63a5\u5230MongoDB\u6570\u636e\u5e93\nclient = MongoClient('mongodb:\/\/localhost:27017\/')\n\n# \u9009\u62e9\u6570\u636e\u5e93\u548c\u96c6\u5408\ndb = client['mydatabase']\ncollection = db['mycollection']\n\n# \u63d2\u5165\u6570\u636e\ntry:\n    collection.insert_one({\"name\": \"John Doe\", \"email\": \"johndoe@example.com\"})\nexcept Exception as e:\n    if \"StringLengthError\" in str(e):\n        print(\"\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\")\n        # \u5904\u7406\u9519\u8bef\u7684\u903b\u8f91\n    else:\n        raise e\n<\/code><\/pre>\n<h2 id=\"%e6%80%bb%e7%bb%93-DYzwnuqpvL\">\u603b\u7ed3<\/h2>\n<p>\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548\u901a\u5e38\u8868\u793a\u5b57\u6bb5\u7684\u503c\u8d85\u8fc7\u4e86\u6700\u5927\u957f\u5ea6\u9650\u5236\u3002\u8981\u4fee\u590d\u8fd9\u4e2a\u9519\u8bef\uff0c\u60a8\u5e94\u8be5\u68c0\u67e5\u6570\u636e\u6a21\u578b\u3001\u9a8c\u8bc1\u8f93\u5165\u6570\u636e\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u6b63\u786e\u5904\u7406\u9519\u8bef\u3002\u5982\u679c\u60a8\u6b63\u5728\u5bfb\u627e\u53ef\u9760\u7684MongoDB\u6258\u7ba1\u89e3\u51b3\u65b9\u6848\uff0c\u9999\u6e2f\u670d\u52a1\u5668\u9996\u9009\u540e\u6d6a\u4e91\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u60a8\u53ef\u4ee5\u5728<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u9999\u6e2f\u670d\u52a1\u5668\u3001\u7f8e\u56fd\u670d\u52a1\u5668\u548c\u4e91\u670d\u52a1\u5668\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u590dMongoDB\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548 \u5982\u4f55\u4fee\u590dMongoDB\u9519\u8bef\u4ee3\u7801 - 16 - \u957f\u5ea6\u65e0\u6548 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-247438","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/247438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=247438"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/247438\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=247438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=247438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=247438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}