{"id":2504,"date":"2025-03-09T09:46:21","date_gmt":"2025-03-09T01:46:21","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2504"},"modified":"2025-03-09T12:59:26","modified_gmt":"2025-03-09T04:59:26","slug":"mongodb-gridfs","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/09\/mongodb-gridfs\/","title":{"rendered":"MongoDB GridFS"},"content":{"rendered":"\n<p>GridFS \u662f MongoDB \u63d0\u4f9b\u7684\u4e00\u4e2a\u6587\u4ef6\u5b58\u50a8\u7cfb\u7edf\uff0c\u7528\u4e8e\u5b58\u50a8\u5927\u4e8e 16 MB \u7684\u6587\u4ef6\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMongoDB \u5b58\u50a8\u7684\u6570\u636e\u6700\u5927\u4e3a 16 MB\uff0c\u4f46 GridFS \u5141\u8bb8\u4f60\u5c06\u66f4\u5927\u7684\u6587\u4ef6\u5206\u5272\u6210\u591a\u4e2a\u5c0f\u5757\uff08\u79f0\u4e3a chunks\uff09\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5c0f\u5757\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u6587\u6863\u4e2d\u3002GridFS \u7684\u5b9e\u73b0\u65b9\u5f0f\u4f7f\u5f97 MongoDB \u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u5e76\u4e14\u80fd\u591f\u652f\u6301\u6587\u4ef6\u7684\u5206\u7247\u3001\u6d41\u5f0f\u4f20\u8f93\u548c\u68c0\u7d22\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. GridFS \u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/h2>\n\n\n\n<p>GridFS \u901a\u8fc7\u5c06\u5927\u6587\u4ef6\u62c6\u5206\u4e3a\u591a\u4e2a &#8220;\u5757&#8221;\uff08chunks\uff09\u6765\u5b58\u50a8\u3002\u6bcf\u4e2a\u5757\u7684\u9ed8\u8ba4\u5927\u5c0f\u662f 255 KB\uff0c\u6587\u4ef6\u4fe1\u606f\u5219\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5355\u72ec\u7684 &#8220;\u6587\u4ef6&#8221; \u6587\u6863\u4e2d\u3002\u6587\u4ef6\u7684\u5b9e\u9645\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u540d\u4e3a <code>fs.chunks<\/code> \u7684\u96c6\u5408\u4e2d\uff0c\u6bcf\u4e2a\u5757\u90fd\u5b58\u50a8\u4e3a\u4e00\u4e2a\u6587\u6863\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6587\u4ef6\u6587\u6863<\/strong>\uff1a\u5b58\u50a8\u6587\u4ef6\u7684\u5143\u6570\u636e\uff0c\u5982\u6587\u4ef6\u7684\u540d\u79f0\u3001\u4e0a\u4f20\u65f6\u95f4\u3001\u6587\u4ef6\u5927\u5c0f\u7b49\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u5757\u6587\u6863<\/strong>\uff1a\u5b58\u50a8\u6587\u4ef6\u7684\u6bcf\u4e2a\u5757\u6570\u636e\u3002\u6bcf\u4e2a\u5757\u90fd\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u6863\uff0c\u5305\u542b\u4e00\u4e2a <code>data<\/code> \u5b57\u6bb5\u548c\u5176\u4ed6\u7528\u4e8e\u8ddf\u8e2a\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u5982\u5757\u7684\u987a\u5e8f\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. GridFS \u4e3b\u8981\u7279\u6027<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u652f\u6301\u5927\u6587\u4ef6<\/strong>\uff1aGridFS \u5141\u8bb8\u5b58\u50a8\u5927\u4e8e 16 MB \u7684\u6587\u4ef6\u3002\u901a\u8fc7\u5c06\u6587\u4ef6\u62c6\u5206\u4e3a\u591a\u4e2a\u5757\u8fdb\u884c\u5b58\u50a8\uff0cMongoDB \u53ef\u4ee5\u5b58\u50a8\u975e\u5e38\u5927\u7684\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u5206\u5272\u548c\u5408\u5e76\u6587\u4ef6<\/strong>\uff1a\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0cGridFS \u4f1a\u81ea\u52a8\u5c06\u6587\u4ef6\u5206\u5272\u4e3a\u5757\uff0c\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u4f1a\u5c06\u8fd9\u4e9b\u5757\u91cd\u65b0\u7ec4\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u6548\u5b58\u50a8<\/strong>\uff1aGridFS \u5728\u5b58\u50a8\u548c\u68c0\u7d22\u6587\u4ef6\u65f6\u63d0\u4f9b\u4e86\u5f88\u9ad8\u7684\u6548\u7387\uff0c\u80fd\u591f\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u6587\u4ef6\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u7684\u6587\u4ef6\u8bbf\u95ee<\/strong>\uff1aGridFS \u63d0\u4f9b\u4e86\u57fa\u4e8e\u6587\u4ef6\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u6309\u9700\u6d41\u5f0f\u5904\u7406\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528 GridFS \u5b58\u50a8\u6587\u4ef6<\/strong><\/h2>\n\n\n\n<p>\u5728 MongoDB \u4e2d\u4f7f\u7528 GridFS \u5b58\u50a8\u6587\u4ef6\u9700\u8981\u4f7f\u7528 MongoDB \u7684\u5ba2\u6237\u7aef\u5e93\uff08\u5982 Python\u3001Node.js \u7b49\uff09\u63d0\u4f9b\u7684 GridFS API\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Python <code>pymongo<\/code> \u5e93\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 MongoDB \u4e2d\u4f7f\u7528 GridFS \u5b58\u50a8\u548c\u68c0\u7d22\u6587\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(1) \u4f7f\u7528 Python \u5b58\u50a8\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86 <code>pymongo<\/code> \u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pymongo<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230 MongoDB GridFS \u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pymongo import MongoClient\nimport gridfs\n\n# \u8fde\u63a5 MongoDB\nclient = MongoClient('mongodb:\/\/localhost:27017\/')\ndb = client&#91;'mydatabase']  # \u9009\u62e9\u6570\u636e\u5e93\nfs = gridfs.GridFS(db)  # \u521b\u5efa GridFS \u5bf9\u8c61\n\n# \u6253\u5f00\u6587\u4ef6\u5e76\u5b58\u50a8\u5230 GridFS\nwith open('example_file.txt', 'rb') as file:\n    file_id = fs.put(file, filename=\"example_file.txt\")\n\nprint(f\"File uploaded with id: {file_id}\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(2) \u4f7f\u7528 Python \u68c0\u7d22\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u8981\u4ece GridFS \u4e2d\u8bfb\u53d6\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u6587\u4ef6 ID \u4ece GridFS \u83b7\u53d6\u6587\u4ef6\nfile = fs.get(file_id)\n\n# \u5c06\u6587\u4ef6\u5185\u5bb9\u5199\u5165\u5230\u672c\u5730\nwith open('downloaded_example_file.txt', 'wb') as output_file:\n    output_file.write(file.read())\n\nprint(f\"File downloaded as 'downloaded_example_file.txt'\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(3) \u4f7f\u7528 Node.js \u5b58\u50a8\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528 MongoDB Node.js \u9a71\u52a8\u548c <code>gridfs-stream<\/code> \u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230 GridFS\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install mongodb gridfs-stream<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230 MongoDB\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const MongoClient = require('mongodb').MongoClient;\nconst fs = require('fs');\nconst Grid = require('gridfs-stream');\n\nconst url = 'mongodb:\/\/localhost:27017';\nconst dbName = 'mydatabase';\n\n\/\/ \u8fde\u63a5\u5230 MongoDB\nMongoClient.connect(url, (err, client) =&gt; {\n  if (err) throw err;\n\n  const db = client.db(dbName);\n  const gfs = Grid(db, MongoClient);\n\n  \/\/ \u6253\u5f00\u6587\u4ef6\u6d41\n  const writeStream = gfs.createWriteStream({\n    filename: 'example_file.txt'\n  });\n\n  \/\/ \u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u5e76\u4e0a\u4f20\u5230 MongoDB\n  fs.createReadStream('example_file.txt').pipe(writeStream);\n\n  writeStream.on('close', (file) =&gt; {\n    console.log('File uploaded to GridFS with id:', file._id);\n    client.close();\n  });\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528 GridFS \u7ba1\u7406\u6587\u4ef6<\/strong><\/h2>\n\n\n\n<p>GridFS \u5141\u8bb8\u6211\u4eec\u5bf9\u5b58\u50a8\u7684\u6587\u4ef6\u8fdb\u884c\u7ba1\u7406\u64cd\u4f5c\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5220\u9664\u6587\u4ef6<\/strong>\uff1a\u901a\u8fc7\u6587\u4ef6 ID \u6216\u6587\u4ef6\u540d\u5220\u9664\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u67e5\u770b\u6587\u4ef6\u5143\u6570\u636e<\/strong>\uff1a\u67e5\u770b\u6587\u4ef6\u7684\u5927\u5c0f\u3001\u4e0a\u4f20\u65f6\u95f4\u3001\u6587\u4ef6\u7c7b\u578b\u7b49\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(1) \u5220\u9664\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5220\u9664\u6587\u4ef6\nfs.delete(file_id)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(2) \u67e5\u770b\u6587\u4ef6\u5143\u6570\u636e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u83b7\u53d6\u6587\u4ef6\u7684\u5143\u6570\u636e\nfile = fs.get(file_id)\nprint(f\"Filename: {file.filename}\")\nprint(f\"Length: {file.length}\")\nprint(f\"Upload Date: {file.upload_date}\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. GridFS \u7684\u4f18\u7f3a\u70b9<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4f18\u70b9<\/strong>\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u652f\u6301\u5927\u6587\u4ef6<\/strong>\uff1a\u80fd\u591f\u5b58\u50a8\u8d85\u8fc7 16 MB \u7684\u6587\u4ef6\uff0c\u9002\u7528\u4e8e\u5927\u6587\u4ef6\u5b58\u50a8\u548c\u6d41\u5f0f\u6570\u636e\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u6548\u7684\u5b58\u50a8\u65b9\u5f0f<\/strong>\uff1a\u901a\u8fc7\u5206\u5757\u5b58\u50a8\u6587\u4ef6\uff0c\u80fd\u591f\u6709\u6548\u7ba1\u7406\u6587\u4ef6\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/li>\n\n\n\n<li><strong>\u4e0e MongoDB \u96c6\u6210<\/strong>\uff1aGridFS \u662f MongoDB \u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5b83\u80fd\u591f\u4eab\u53d7 MongoDB \u63d0\u4f9b\u7684\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u6269\u5c55\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7f3a\u70b9<\/strong>\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u76f8\u5bf9\u590d\u6742\u7684\u7ba1\u7406<\/strong>\uff1a\u4e0e\u666e\u901a\u7684 MongoDB \u6587\u6863\u5b58\u50a8\u76f8\u6bd4\uff0cGridFS \u5728\u5b58\u50a8\u548c\u7ba1\u7406\u6587\u4ef6\u65f6\u9700\u8981\u66f4\u591a\u7684\u64cd\u4f5c\uff0c\u5982\u5206\u5272\u6587\u4ef6\u3001\u7ba1\u7406\u5757\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u9002\u5408\u5c0f\u6587\u4ef6<\/strong>\uff1a\u5bf9\u4e8e\u5c0f\u4e8e 16 MB \u7684\u6587\u4ef6\uff0c\u76f4\u63a5\u5b58\u50a8\u5728 MongoDB \u4e2d\u66f4\u52a0\u7b80\u5355\u9ad8\u6548\uff0c\u800c\u4f7f\u7528 GridFS \u53ef\u80fd\u4f1a\u589e\u52a0\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>GridFS \u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684 MongoDB \u529f\u80fd\uff0c\u4e13\u95e8\u7528\u4e8e\u5b58\u50a8\u5927\u4e8e 16 MB \u7684\u6587\u4ef6\u3002\u5b83\u901a\u8fc7\u5c06\u6587\u4ef6\u5206\u5272\u4e3a\u591a\u4e2a\u5757\u5e76\u5b58\u50a8\u5728 MongoDB \u4e2d\uff0c\u5141\u8bb8\u7528\u6237\u6709\u6548\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u5927\u6587\u4ef6\u3002GridFS \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\u6765\u5b58\u50a8\u3001\u68c0\u7d22\u548c\u7ba1\u7406\u6587\u4ef6\uff0c\u4f7f MongoDB \u80fd\u591f\u6210\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u6587\u4ef6\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u9700\u8981\u5b58\u50a8\u5927\u91cf\u5927\u6587\u4ef6\uff0cGridFS \u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GridFS \u662f MongoDB \u63d0\u4f9b\u7684\u4e00\u4e2a\u6587\u4ef6\u5b58\u50a8\u7cfb\u7edf\uff0c\u7528\u4e8e\u5b58\u50a8\u5927\u4e8e 16 MB \u7684\u6587\u4ef6\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMon [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2534,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-2504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2504"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2504\/revisions"}],"predecessor-version":[{"id":2505,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2504\/revisions\/2505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2534"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}