{"id":5385,"date":"2024-11-14T12:17:04","date_gmt":"2024-11-14T04:17:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5385\/"},"modified":"2024-11-14T12:17:04","modified_gmt":"2024-11-14T04:17:04","slug":"mongodb%e6%80%8e%e4%b9%88%e6%89%a7%e8%a1%8c%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5385\/","title":{"rendered":"mongodb\u600e\u4e48\u6267\u884c\u547d\u4ee4"},"content":{"rendered":"<blockquote><p>\n  mongodb\u547d\u4ee4\u6267\u884c\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u4f7f\u7528mongo shell\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u754c\u9762\u4f7f\u7528mongodb\u9a71\u52a8\u7a0b\u5e8f\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5\u6216\u51fd\u6570\u4f7f\u7528http\u63a5\u53e3\u901a\u8fc7http\u8bf7\u6c42\u53d1\u9001\u547d\u4ee4\u4f7f\u7528mongodb compass\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\u5728\u201c\u67e5\u8be2\u201d\u3001\u201c\u805a\u5408\u201d\u6216\u201c\u811a\u672c\u201d\u9009\u9879\u5361\u4e2d\u6267\u884c\u547d\u4ee4\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052803152634725.jpg\" class=\"aligncenter\" title=\"mongodb\u600e\u4e48\u6267\u884c\u547d\u4ee4\u63d2\u56fe\" alt=\"mongodb\u600e\u4e48\u6267\u884c\u547d\u4ee4\u63d2\u56fe\" \/><\/p>\n<p><strong>MongoDB\u547d\u4ee4\u6267\u884c\u65b9\u6cd5<\/strong><\/p>\n<p>MongoDB\u63d0\u4f9b\u4e86\u51e0\u79cd\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528mongo shell<\/strong><\/p>\n<p>mongo shell\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u754c\u9762\uff0c\u53ef\u7528\u4e8e\u8fde\u63a5\u5230MongoDB\u6570\u636e\u5e93\u5e76\u6267\u884c\u547d\u4ee4\u3002\u8981\u6267\u884c\u547d\u4ee4\uff0c\u8bf7\u952e\u5165\u547d\u4ee4\u5e76\u6309Enter\u952e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>&gt; db.collection.find()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528MongoDB\u9a71\u52a8\u7a0b\u5e8f<\/strong><\/p>\n<p>MongoDB\u63d0\u4f9b\u4e86\u5404\u79cd\u8bed\u8a00\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002\u9a71\u52a8\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5\u6216\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5728Node.js\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u547d\u4ee4\uff1a<\/p>\n<pre>const MongoClient = require('mongodb').MongoClient;\n\nMongoClient.connect(url, function(err, db) {\n  if (err) throw err;\n\n  db.collection('collection').find({}, function(err, result) {\n    if (err) throw err;\n\n    console.log(result);\n  });\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528HTTP\u63a5\u53e3<\/strong><\/p>\n<p>MongoDB\u8fd8\u63d0\u4f9b\u4e86HTTP\u63a5\u53e3\uff0c\u5141\u8bb8\u901a\u8fc7HTTP\u8bf7\u6c42\u53d1\u9001\u547d\u4ee4\u3002\u8981\u6267\u884c\u547d\u4ee4\uff0c\u8bf7\u5411\u6307\u5b9a\u7aef\u70b9\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u5305\u542b\u547d\u4ee4\u4f5c\u4e3a\u8bf7\u6c42\u6b63\u6587\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528cURL\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6267\u884c\u67e5\u627e\u547d\u4ee4\uff1a<\/p>\n<pre>curl -X POST -H \"Content-Type: application\/json\" -d '{\"find\": \"collection\", \"filter\": {}}' http:\/\/localhost:27017\/db<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528MongoDB Compass<\/strong><\/p>\n<p>MongoDB Compass\u662f\u4e00\u4e2a\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\uff0c\u53ef\u7528\u4e8e\u8fde\u63a5\u5230MongoDB\u6570\u636e\u5e93\u5e76\u6267\u884c\u547d\u4ee4\u3002\u53ef\u4ee5\u5728MongoDB Compass\u4e2d\u6267\u884c\u547d\u4ee4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<ul>\n<li>\u5728\u201c\u67e5\u8be2\u201d\u9009\u9879\u5361\u4e2d\uff0c\u952e\u5165\u547d\u4ee4\u5e76\u6309\u201c\u6267\u884c\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u805a\u5408\u201d\u9009\u9879\u5361\u4e2d\uff0c\u7f16\u5199\u805a\u5408\u7ba1\u9053\u5e76\u6309\u201c\u6267\u884c\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u811a\u672c\u201d\u9009\u9879\u5361\u4e2d\uff0c\u7f16\u5199JavaScript\u811a\u672c\u5e76\u6309\u201c\u8fd0\u884c\u201d\u6309\u94ae\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u6267\u884c\u547d\u4ee4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mongodb\u547d\u4ee4\u6267\u884c\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u4f7f\u7528mongo shell\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u754c\u9762\u4f7f\u7528mongodb\u9a71\u52a8\u7a0b\u5e8f\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5\u6216\u51fd\u6570\u4f7f\u7528http\u63a5\u53e3\u901a\u8fc7http\u8bf7\u6c42\u53d1\u9001\u547d\u4ee4\u4f7f\u7528mongodb compass\u56fe\u5f62\u5316\u7528\u6237\u754c\u9762\u5728\u201c\u67e5\u8be2\u201d\u3001\u201c\u805a\u5408\u201d\u6216\u201c\u811a\u672c\u201d\u9009\u9879\u5361\u4e2d\u6267\u884c\u547d\u4ee4 MongoDB\u547d\u4ee4\u6267\u884c\u65b9\u6cd5 MongoDB\u63d0\u4f9b\u4e86\u51e0\u79cd\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\uff1a 1. \u4f7f\u7528mongo shell mongo shell\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u754c\u9762\uff0c\u53ef\u7528\u4e8e\u8fde\u63a5\u5230MongoDB\u6570\u636e\u5e93\u5e76\u6267\u884c\u547d\u4ee4\u3002\u8981\u6267\u884c\u547d\u4ee4\uff0c\u8bf7\u952e\u5165\u547d\u4ee4\u5e76\u6309Enter\u952e\u3002\u4f8b\u5982\uff1a &gt; db.collection.find() \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528MongoDB\u9a71\u52a8\u7a0b\u5e8f MongoDB\u63d0\u4f9b\u4e86\u5404\u79cd\u8bed\u8a00\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002\u9a71\u52a8\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5\u6216\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5728Node.js\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6267\u884c\u547d\u4ee4\uff1a const MongoClient = require(&#8216;mongodb&#8217;).MongoClient; MongoClient.connect(url, function(err, db) { if (err) throw err; db.collection(&#8216;collection&#8217;).find({}, function(err, result) { if (err) throw err; console.log(result); }); }); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528HTTP\u63a5\u53e3 MongoDB\u8fd8\u63d0\u4f9b\u4e86HTTP\u63a5\u53e3\uff0c\u5141\u8bb8\u901a\u8fc7HTTP\u8bf7\u6c42\u53d1\u9001\u547d\u4ee4\u3002\u8981\u6267\u884c\u547d\u4ee4\uff0c\u8bf7\u5411\u6307\u5b9a\u7aef\u70b9\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u5305\u542b\u547d\u4ee4\u4f5c\u4e3a\u8bf7\u6c42\u6b63\u6587\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528cURL\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6267\u884c\u67e5\u627e\u547d\u4ee4\uff1a curl -X POST -H &#8220;Content-Type: application\/json&#8221; -d &#8216;{&#8220;find&#8221;: &#8220;collection&#8221;, &#8220;filter&#8221;: {}}&#8217; http:\/\/localhost:27017\/db [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5385","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5385"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5385\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}