{"id":63944,"date":"2024-10-14T03:48:15","date_gmt":"2024-10-13T19:48:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/63944\/"},"modified":"2024-10-14T03:48:15","modified_gmt":"2024-10-13T19:48:15","slug":"mongodb%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/63944\/","title":{"rendered":"MongoDB\u5beb\u5165\u6578\u64da\u7b56\u7565"},"content":{"rendered":"<h1 id=\"mongodb%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e7%ad%96%e7%95%a5-CKeplxEtcY\">MongoDB\u5beb\u5165\u6578\u64da\u7b56\u7565<\/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\u56e0\u5176\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u975e\u7d50\u69cb\u5316\u6578\u64da\u6642\uff0cMongoDB\u7684\u5beb\u5165\u6578\u64da\u7b56\u7565\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMongoDB\u7684\u5beb\u5165\u6578\u64da\u7b56\u7565\uff0c\u5305\u62ec\u5beb\u5165\u6a21\u5f0f\u3001\u4e8b\u52d9\u8655\u7406\u3001\u4ee5\u53ca\u5982\u4f55\u512a\u5316\u5beb\u5165\u6027\u80fd\u3002<\/p>\n<h2 id=\"mongodb%e7%9a%84%e5%af%ab%e5%85%a5%e6%a8%a1%e5%bc%8f-CKeplxEtcY\">MongoDB\u7684\u5beb\u5165\u6a21\u5f0f<\/h2>\n<p>MongoDB\u63d0\u4f9b\u4e86\u591a\u7a2e\u5beb\u5165\u6a21\u5f0f\uff0c\u9019\u4e9b\u6a21\u5f0f\u53ef\u4ee5\u6839\u64da\u61c9\u7528\u9700\u6c42\u9032\u884c\u8abf\u6574\u3002\u4e3b\u8981\u7684\u5beb\u5165\u6a21\u5f0f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u55ae\u5beb\u5165\u6a21\u5f0f<\/strong>: \u5728\u9019\u7a2e\u6a21\u5f0f\u4e0b\uff0c\u6578\u64da\u5c07\u88ab\u5beb\u5165\u5230\u55ae\u500b\u6578\u64da\u5eab\u5be6\u4f8b\u3002\u9019\u662f\u6700\u57fa\u672c\u7684\u5beb\u5165\u65b9\u5f0f\uff0c\u9069\u5408\u5c0f\u578b\u61c9\u7528\u3002<\/li>\n<li><strong>\u591a\u5beb\u5165\u6a21\u5f0f<\/strong>: \u7576\u9700\u8981\u5c07\u6578\u64da\u5beb\u5165\u591a\u500b\u526f\u672c\u6642\uff0c\u9019\u7a2e\u6a21\u5f0f\u6703\u88ab\u4f7f\u7528\u3002\u9019\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u6578\u64da\u5197\u9918\u7684\u61c9\u7528\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<li><strong>\u6279\u91cf\u5beb\u5165\u6a21\u5f0f<\/strong>: \u9019\u7a2e\u6a21\u5f0f\u5141\u8a31\u4e00\u6b21\u6027\u5beb\u5165\u591a\u689d\u6578\u64da\uff0c\u5f9e\u800c\u63d0\u9ad8\u5beb\u5165\u6548\u7387\u3002\u4f7f\u7528\u6279\u91cf\u5beb\u5165\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u7db2\u7d61\u5ef6\u9072\u548c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%ba%8b%e5%8b%99%e8%99%95%e7%90%86-CKeplxEtcY\">\u4e8b\u52d9\u8655\u7406<\/h2>\n<p>MongoDB\u57284.0\u7248\u672c\u4e2d\u5f15\u5165\u4e86\u591a\u6587\u6a94\u4e8b\u52d9\uff0c\u9019\u4f7f\u5f97\u5728\u591a\u500b\u6587\u6a94\u4e4b\u9593\u9032\u884c\u539f\u5b50\u64cd\u4f5c\u6210\u70ba\u53ef\u80fd\u3002\u9019\u5c0d\u65bc\u9700\u8981\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u7684\u61c9\u7528\u81f3\u95dc\u91cd\u8981\u3002\u4e8b\u52d9\u7684\u57fa\u672c\u7279\u6027\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u539f\u5b50\u6027<\/strong>: \u4e8b\u52d9\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u8981\u9ebc\u5168\u90e8\u6210\u529f\uff0c\u8981\u9ebc\u5168\u90e8\u5931\u6557\u3002<\/li>\n<li><strong>\u9694\u96e2\u6027<\/strong>: \u4e8b\u52d9\u4e4b\u9593\u7684\u64cd\u4f5c\u4e0d\u6703\u76f8\u4e92\u5e72\u64fe\u3002<\/li>\n<li><strong>\u6301\u4e45\u6027<\/strong>: \u4e00\u65e6\u4e8b\u52d9\u63d0\u4ea4\uff0c\u6578\u64da\u5c07\u6c38\u4e45\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4e8b\u52d9\u793a\u4f8b\uff1a<\/p>\n<pre><code>const session = client.startSession();\nsession.startTransaction();\ntry {\n    await collection1.insertOne({ name: \"Alice\" }, { session });\n    await collection2.insertOne({ name: \"Bob\" }, { session });\n    await session.commitTransaction();\n} catch (error) {\n    await session.abortTransaction();\n} finally {\n    session.endSession();\n}<\/code><\/pre>\n<h2 id=\"%e5%84%aa%e5%8c%96%e5%af%ab%e5%85%a5%e6%80%a7%e8%83%bd-CKeplxEtcY\">\u512a\u5316\u5beb\u5165\u6027\u80fd<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8MongoDB\u7684\u5beb\u5165\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u500b\u7b56\u7565\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u7d22\u5f15<\/strong>: \u96d6\u7136\u7d22\u5f15\u53ef\u4ee5\u52a0\u5feb\u67e5\u8a62\u901f\u5ea6\uff0c\u4f46\u904e\u591a\u7684\u7d22\u5f15\u6703\u5f71\u97ff\u5beb\u5165\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u61c9\u6839\u64da\u5be6\u969b\u9700\u6c42\u5408\u7406\u8a2d\u7f6e\u7d22\u5f15\u3002<\/li>\n<li><strong>\u8abf\u6574\u5beb\u5165\u95dc\u9589\u7b56\u7565<\/strong>: MongoDB\u5141\u8a31\u7528\u6236\u8a2d\u7f6e\u5beb\u5165\u95dc\u9589\u7b56\u7565\uff0c\u5982\u201cacknowledged\u201d\u3001\u201cunacknowledged\u201d\u7b49\u3002\u6839\u64da\u61c9\u7528\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u7b56\u7565\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528Replica Set<\/strong>: \u5728Replica Set\u4e2d\uff0c\u4e3b\u7bc0\u9ede\u8ca0\u8cac\u5beb\u5165\u64cd\u4f5c\uff0c\u800c\u5f9e\u7bc0\u9ede\u5247\u7528\u65bc\u8b80\u53d6\u64cd\u4f5c\uff0c\u9019\u6a23\u53ef\u4ee5\u5206\u6563\u8ca0\u8f09\uff0c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-CKeplxEtcY\">\u7d50\u8ad6<\/h2>\n<p>MongoDB\u7684\u5beb\u5165\u6578\u64da\u7b56\u7565\u5c0d\u65bc\u78ba\u4fdd\u6578\u64da\u7684\u53ef\u9760\u6027\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u9078\u64c7\u5408\u9069\u7684\u5beb\u5165\u6a21\u5f0f\u3001\u5229\u7528\u4e8b\u52d9\u8655\u7406\u4ee5\u53ca\u512a\u5316\u5beb\u5165\u6027\u80fd\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u9019\u4e9b\u7b56\u7565\u5c07\u5e6b\u52a9\u4f01\u696d\u5728\u7af6\u722d\u4e2d\u4fdd\u6301\u512a\u52e2\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684MongoDB\u6578\u64da\u5eab\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MongoDB\u7684\u5beb\u5165\u6578\u64da\u7b56\u7565\uff0c\u63d0\u5347\u6578\u64da\u4e00\u81f4\u6027\u8207\u6027\u80fd\uff0c\u78ba\u4fdd\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u8207\u5b58\u5132\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-63944","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63944","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=63944"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63944\/revisions"}],"predecessor-version":[{"id":63945,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63944\/revisions\/63945"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=63944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=63944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=63944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}