{"id":63692,"date":"2024-10-14T01:40:15","date_gmt":"2024-10-13T17:40:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/63692\/"},"modified":"2024-10-14T01:40:16","modified_gmt":"2024-10-13T17:40:16","slug":"mongodb%e8%a1%93%e8%aa%9e-%e8%ae%8a%e6%9b%b4%e6%b5%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/63692\/","title":{"rendered":"MongoDB\u8853\u8a9e &#8211; \u8b8a\u66f4\u6d41"},"content":{"rendered":"<h1 id=\"mongodb%e8%a1%93%e8%aa%9e-%e8%ae%8a%e6%9b%b4%e6%b5%81-irwbQJyMaA\">MongoDB\u8853\u8a9e &#8211; \u8b8a\u66f4\u6d41<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0cMongoDB\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684NoSQL\u6578\u64da\u5eab\uff0c\u8d8a\u4f86\u8d8a\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u5176\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\u548c\u9ad8\u6548\u7684\u67e5\u8a62\u80fd\u529b\u4f7f\u5176\u6210\u70ba\u8a31\u591a\u61c9\u7528\u7a0b\u5e8f\u7684\u9996\u9078\u3002\u800c\u5728MongoDB\u4e2d\uff0c\u8b8a\u66f4\u6d41\uff08Change Streams\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5be6\u6642\u76e3\u63a7\u6578\u64da\u7684\u8b8a\u66f4\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e8%ae%8a%e6%9b%b4%e6%b5%81%ef%bc%9f-irwbQJyMaA\">\u4ec0\u9ebc\u662f\u8b8a\u66f4\u6d41\uff1f<\/h2>\n<p>\u8b8a\u66f4\u6d41\u662f\u4e00\u7a2eMongoDB\u7684\u529f\u80fd\uff0c\u5141\u8a31\u61c9\u7528\u7a0b\u5e8f\u76e3\u807d\u6578\u64da\u5eab\u4e2d\u96c6\u5408\u7684\u8b8a\u66f4\u4e8b\u4ef6\u3002\u9019\u4e9b\u8b8a\u66f4\u4e8b\u4ef6\u5305\u62ec\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002\u901a\u904e\u8b8a\u66f4\u6d41\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5be6\u6642\u7372\u53d6\u6578\u64da\u8b8a\u66f4\u7684\u901a\u77e5\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5373\u6642\u53cd\u61c9\u7684\u61c9\u7528\u7a0b\u5e8f\u4f86\u8aaa\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e8%ae%8a%e6%9b%b4%e6%b5%81%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86-irwbQJyMaA\">\u8b8a\u66f4\u6d41\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u8b8a\u66f4\u6d41\u57fa\u65bcMongoDB\u7684\u805a\u5408\u6846\u67b6\uff0c\u4f7f\u7528\u4e86\u201coplog\u201d\uff08\u64cd\u4f5c\u65e5\u8a8c\uff09\u4f86\u8ffd\u8e64\u6578\u64da\u7684\u8b8a\u66f4\u3002\u7576\u6578\u64da\u5728\u96c6\u5408\u4e2d\u767c\u751f\u8b8a\u66f4\u6642\uff0cMongoDB\u6703\u5c07\u9019\u4e9b\u8b8a\u66f4\u8a18\u9304\u5230oplog\u4e2d\u3002\u8b8a\u66f4\u6d41\u901a\u904e\u805a\u5408\u7ba1\u9053\u4f86\u904e\u6ffe\u548c\u683c\u5f0f\u5316\u9019\u4e9b\u8b8a\u66f4\u4e8b\u4ef6\uff0c\u4e26\u5c07\u5176\u767c\u9001\u7d66\u8a02\u95b1\u8005\u3002<\/p>\n<h3 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e8%ae%8a%e6%9b%b4%e6%b5%81-irwbQJyMaA\">\u5982\u4f55\u4f7f\u7528\u8b8a\u66f4\u6d41<\/h3>\n<p>\u8981\u4f7f\u7528\u8b8a\u66f4\u6d41\uff0c\u958b\u767c\u8005\u9700\u8981\u5728MongoDB\u4e2d\u5efa\u7acb\u4e00\u500b\u76e3\u807d\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528\u8b8a\u66f4\u6d41\uff1a<\/p>\n<pre><code>const { MongoClient } = require('mongodb');\n\nasync function main() {\n    const uri = 'mongodb:\/\/localhost:27017';\n    const client = new MongoClient(uri);\n\n    try {\n        await client.connect();\n        const database = client.db('testdb');\n        const collection = database.collection('testcollection');\n\n        \/\/ \u5efa\u7acb\u8b8a\u66f4\u6d41\n        const changeStream = collection.watch();\n\n        \/\/ \u76e3\u807d\u8b8a\u66f4\u4e8b\u4ef6\n        changeStream.on('change', (change) =&gt; {\n            console.log('\u8b8a\u66f4\u4e8b\u4ef6:', change);\n        });\n    } finally {\n        \/\/ client.close(); \/\/ \u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u61c9\u6839\u64da\u9700\u8981\u95dc\u9589\u9023\u63a5\n    }\n}\n\nmain().catch(console.error);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230MongoDB\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u5c0d\u6307\u5b9a\u7684\u96c6\u5408\u5efa\u7acb\u8b8a\u66f4\u6d41\u3002\u7576\u96c6\u5408\u4e2d\u7684\u6578\u64da\u767c\u751f\u8b8a\u66f4\u6642\uff0c\u76e3\u807d\u5668\u6703\u6355\u7372\u5230\u9019\u4e9b\u8b8a\u66f4\u4e8b\u4ef6\u4e26\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2 id=\"%e8%ae%8a%e6%9b%b4%e6%b5%81%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-irwbQJyMaA\">\u8b8a\u66f4\u6d41\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>\u8b8a\u66f4\u6d41\u7684\u61c9\u7528\u5834\u666f\u975e\u5e38\u5ee3\u6cdb\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f8b\u5b50\uff1a<\/p>\n<ul>\n<li><strong>\u5373\u6642\u901a\u77e5\u7cfb\u7d71\uff1a<\/strong>\u7576\u6578\u64da\u767c\u751f\u8b8a\u66f4\u6642\uff0c\u7cfb\u7d71\u53ef\u4ee5\u5373\u6642\u901a\u77e5\u7528\u6236\uff0c\u4f8b\u5982\u5728\u793e\u4ea4\u5a92\u9ad4\u5e73\u53f0\u4e0a\uff0c\u7576\u6709\u65b0\u6d88\u606f\u6216\u8a55\u8ad6\u6642\uff0c\u7cfb\u7d71\u53ef\u4ee5\u5373\u6642\u63a8\u9001\u901a\u77e5\u3002<\/li>\n<li><strong>\u6578\u64da\u540c\u6b65\uff1a<\/strong>\u5728\u591a\u500b\u6578\u64da\u5eab\u4e4b\u9593\u9032\u884c\u6578\u64da\u540c\u6b65\u6642\uff0c\u8b8a\u66f4\u6d41\u53ef\u4ee5\u5e6b\u52a9\u5be6\u6642\u6355\u7372\u8b8a\u66f4\u4e26\u5c07\u5176\u61c9\u7528\u5230\u5176\u4ed6\u6578\u64da\u5eab\u4e2d\u3002<\/li>\n<li><strong>\u5206\u6790\u548c\u5831\u544a\uff1a<\/strong>\u901a\u904e\u76e3\u63a7\u6578\u64da\u8b8a\u66f4\uff0c\u4f01\u696d\u53ef\u4ee5\u5be6\u6642\u5206\u6790\u6578\u64da\u4e26\u751f\u6210\u5831\u544a\uff0c\u5e6b\u52a9\u6c7a\u7b56\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%ae%8a%e6%9b%b4%e6%b5%81%e7%9a%84%e5%84%aa%e5%8b%a2-irwbQJyMaA\">\u8b8a\u66f4\u6d41\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528\u8b8a\u66f4\u6d41\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5be6\u6642\u6027\uff1a<\/strong>\u8b8a\u66f4\u6d41\u63d0\u4f9b\u4e86\u5373\u6642\u7684\u6578\u64da\u8b8a\u66f4\u901a\u77e5\uff0c\u80fd\u5920\u5feb\u901f\u97ff\u61c9\u7528\u6236\u9700\u6c42\u3002<\/li>\n<li><strong>\u7c21\u5316\u958b\u767c\uff1a<\/strong>\u958b\u767c\u8005\u4e0d\u9700\u8981\u5b9a\u671f\u67e5\u8a62\u6578\u64da\u5eab\u4f86\u6aa2\u67e5\u8b8a\u66f4\uff0c\u6e1b\u5c11\u4e86\u7cfb\u7d71\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u8b8a\u66f4\u6d41\u53ef\u4ee5\u8207\u5176\u4ed6\u6280\u8853\uff08\u5982\u6d88\u606f\u968a\u5217\uff09\u7d50\u5408\u4f7f\u7528\uff0c\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-irwbQJyMaA\">\u7e3d\u7d50<\/h2>\n<p>\u8b8a\u66f4\u6d41\u662fMongoDB\u4e2d\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5be6\u6642\u76e3\u63a7\u6578\u64da\u8b8a\u66f4\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002\u7121\u8ad6\u662f\u5728\u5373\u6642\u901a\u77e5\u7cfb\u7d71\u3001\u6578\u64da\u540c\u6b65\u9084\u662f\u5206\u6790\u5831\u544a\u4e2d\uff0c\u8b8a\u66f4\u6d41\u90fd\u80fd\u767c\u63ee\u91cd\u8981\u4f5c\u7528\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u7a0b\u5e8f\u4f86\u8aaa\uff0c\u4e86\u89e3\u548c\u638c\u63e1\u8b8a\u66f4\u6d41\u7684\u4f7f\u7528\u5c07\u662f\u975e\u5e38\u6709\u76ca\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u60a8\u7684\u61c9\u7528\u4e2d\u5be6\u73feMongoDB\u7684\u8b8a\u66f4\u6d41\u6709\u9032\u4e00\u6b65\u7684\u8208\u8da3\uff0c\u6216\u8005\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\u7684\u8cc7\u8a0a\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MongoDB\u8853\u8a9e\u4e2d\u7684\u8b8a\u66f4\u6d41\uff0c\u63a2\u7d22\u6578\u64da\u8b8a\u66f4\u7684\u5be6\u6642\u6355\u6349\u8207\u8655\u7406\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u4e00\u81f4\u6027\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-63692","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63692","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=63692"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63692\/revisions"}],"predecessor-version":[{"id":63693,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63692\/revisions\/63693"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=63692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=63692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=63692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}