{"id":171871,"date":"2024-11-07T08:21:16","date_gmt":"2024-11-07T00:21:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171871\/"},"modified":"2024-11-07T08:21:17","modified_gmt":"2024-11-07T00:21:17","slug":"%e8%bc%95%e9%ac%86%e5%ad%b8%e7%bf%92%ef%bc%9a%e5%a6%82%e4%bd%95%e6%88%90%e5%8a%9f%e8%aa%bf%e7%94%a8on%e6%95%b8%e6%93%9a%e5%ba%ab-%e6%80%8e%e6%a8%a3%e8%aa%bf%e5%8f%96json%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171871\/","title":{"rendered":"\u8f15\u9b06\u5b78\u7fd2\uff1a\u5982\u4f55\u6210\u529f\u8abf\u7528ON\u6578\u64da\u5eab (\u600e\u6a23\u8abf\u53d6json\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e8%bc%95%e9%ac%86%e5%ad%b8%e7%bf%92%ef%bc%9a%e5%a6%82%e4%bd%95%e6%88%90%e5%8a%9f%e8%aa%bf%e7%94%a8on%e6%95%b8%e6%93%9a%e5%ba%ab-%e6%80%8e%e6%a8%a3%e8%aa%bf%e5%8f%96json%e6%95%b8%e6%93%9a%e5%ba%ab-TPFcGdcbwu\">\u8f15\u9b06\u5b78\u7fd2\uff1a\u5982\u4f55\u6210\u529f\u8abf\u7528ON\u6578\u64da\u5eab (\u600e\u6a23\u8abf\u53d6json\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002ON\u6578\u64da\u5eab\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u7279\u5225\u9069\u5408\u9700\u8981\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u6210\u529f\u8abf\u7528ON\u6578\u64da\u5eab\uff0c\u7279\u5225\u662f\u5982\u4f55\u8abf\u53d6JSON\u6578\u64da\u5eab\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u5229\u7528\u9019\u4e00\u6280\u8853\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afon%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-TPFcGdcbwu\">\u4ec0\u9ebc\u662fON\u6578\u64da\u5eab\uff1f<\/h2>\n<p>ON\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u57fa\u65bc\u6587\u6a94\u7684\u6578\u64da\u5eab\uff0c\u5c08\u9580\u8a2d\u8a08\u7528\u65bc\u5b58\u5132\u548c\u6aa2\u7d22JSON\u683c\u5f0f\u7684\u6578\u64da\u3002\u5b83\u7684\u7d50\u69cb\u9748\u6d3b\uff0c\u5141\u8a31\u7528\u6236\u4ee5\u9375\u503c\u5c0d\u7684\u5f62\u5f0f\u5b58\u5132\u6578\u64da\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u8b8a\u5f97\u66f4\u52a0\u9ad8\u6548\u3002ON\u6578\u64da\u5eab\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6548\u7684\u67e5\u8a62\u6027\u80fd<\/li>\n<li>\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb<\/li>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e8%aa%bf%e7%94%a8on%e6%95%b8%e6%93%9a%e5%ba%ab-TPFcGdcbwu\">\u5982\u4f55\u8abf\u7528ON\u6578\u64da\u5eab<\/h2>\n<p>\u8abf\u7528ON\u6578\u64da\u5eab\u7684\u904e\u7a0b\u53ef\u4ee5\u5206\u70ba\u5e7e\u500b\u6b65\u9a5f\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u64cd\u4f5c\u6d41\u7a0b\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9don%e6%95%b8%e6%93%9a%e5%ba%ab-TPFcGdcbwu\">1. \u5b89\u88ddON\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u60a8\u7684\u4f3a\u670d\u5668\u4e0a\u5b89\u88ddON\u6578\u64da\u5eab\u3002\u53ef\u4ee5\u901a\u904e\u5b98\u65b9\u7db2\u7ad9\u4e0b\u8f09\u5b89\u88dd\u5305\uff0c\u4e26\u6839\u64da\u6307\u793a\u9032\u884c\u5b89\u88dd\u3002\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u555f\u52d5\u6578\u64da\u5eab\u670d\u52d9\u3002<\/p>\n<h3 id=\"2-%e9%80%a3%e6%8e%a5%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-TPFcGdcbwu\">2. \u9023\u63a5\u5230\u6578\u64da\u5eab<\/h3>\n<p>\u4f7f\u7528\u9069\u7576\u7684\u9a45\u52d5\u7a0b\u5e8f\u9023\u63a5\u5230ON\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Python\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import ondb\n\n# \u9023\u63a5\u5230ON\u6578\u64da\u5eab\nconnection = ondb.connect(host='localhost', port=27017, database='your_database')\n<\/code><\/pre>\n<h3 id=\"3-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e9%9b%86%e5%90%88-TPFcGdcbwu\">3. \u5275\u5efa\u6578\u64da\u96c6\u5408<\/h3>\n<p>\u5728ON\u6578\u64da\u5eab\u4e2d\uff0c\u6578\u64da\u4ee5\u96c6\u5408\u7684\u5f62\u5f0f\u5b58\u5132\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u5275\u5efa\u4e00\u500b\u65b0\u7684\u96c6\u5408\uff1a<\/p>\n<pre><code>collection = connection.create_collection('your_collection')\n<\/code><\/pre>\n<h3 id=\"4-%e6%8f%92%e5%85%a5json%e6%95%b8%e6%93%9a-TPFcGdcbwu\">4. \u63d2\u5165JSON\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u96c6\u5408\u5275\u5efa\u5b8c\u6210\uff0c\u60a8\u53ef\u4ee5\u958b\u59cb\u63d2\u5165JSON\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>data = {\n    \"name\": \"John Doe\",\n    \"age\": 30,\n    \"city\": \"Hong Kong\"\n}\n\ncollection.insert_one(data)\n<\/code><\/pre>\n<h3 id=\"5-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-TPFcGdcbwu\">5. \u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u8abf\u53d6\u6578\u64da\u662f\u4f7f\u7528ON\u6578\u64da\u5eab\u7684\u4e3b\u8981\u529f\u80fd\u4e4b\u4e00\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u67e5\u8a62\u6578\u64da\uff1a<\/p>\n<pre><code>result = collection.find({\"name\": \"John Doe\"})\nfor document in result:\n    print(document)\n<\/code><\/pre>\n<h2 id=\"json%e6%95%b8%e6%93%9a%e7%9a%84%e5%84%aa%e5%8b%a2-TPFcGdcbwu\">JSON\u6578\u64da\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528JSON\u683c\u5f0f\u5b58\u5132\u6578\u64da\u6709\u8a31\u591a\u512a\u52e2\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u6613\u65bc\u95b1\u8b80\u548c\u7de8\u8f2f<\/li>\n<li>\u8207\u591a\u7a2e\u7de8\u7a0b\u8a9e\u8a00\u517c\u5bb9<\/li>\n<li>\u652f\u6301\u5d4c\u5957\u7d50\u69cb\uff0c\u9069\u5408\u8907\u96dc\u6578\u64da<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-TPFcGdcbwu\">\u7e3d\u7d50<\/h2>\n<p>\u8abf\u7528ON\u6578\u64da\u5eab\u4e26\u8abf\u53d6JSON\u6578\u64da\u5eab\u7684\u904e\u7a0b\u76f8\u5c0d\u7c21\u55ae\uff0c\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u7121\u8ad6\u662f\u7528\u65bc\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0cON\u6578\u64da\u5eab\u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u6578\u64da\u5eab\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u5b78\u7fd2\u5982\u4f55\u6210\u529f\u8abf\u7528ON\u6578\u64da\u5eab\uff0c\u638c\u63e1\u8abf\u53d6JSON\u6578\u64da\u7684\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u80fd\u529b\uff0c\u7c21\u5316\u5de5\u4f5c\u6d41\u7a0b\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-171871","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171871","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=171871"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171871\/revisions"}],"predecessor-version":[{"id":171872,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171871\/revisions\/171872"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}