{"id":63774,"date":"2024-10-14T02:22:22","date_gmt":"2024-10-13T18:22:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/63774\/"},"modified":"2024-10-14T02:22:22","modified_gmt":"2024-10-13T18:22:22","slug":"mongodb%e8%a1%93%e8%aa%9e-%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/63774\/","title":{"rendered":"MongoDB\u8853\u8a9e &#8211; \u6279\u91cf\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"mongodb%e8%a1%93%e8%aa%9e-%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c-kjLVLYjAQY\">MongoDB\u8853\u8a9e &#8211; \u6279\u91cf\u64cd\u4f5c<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u9078\u64c7\u5c0d\u65bc\u6027\u80fd\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002MongoDB\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684NoSQL\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\u548c\u9ad8\u6548\u7684\u67e5\u8a62\u80fd\u529b\u3002\u5176\u4e2d\uff0c\u6279\u91cf\u64cd\u4f5c\uff08Bulk Operations\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMongoDB\u7684\u6279\u91cf\u64cd\u4f5c\u8853\u8a9e\u53ca\u5176\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c%ef%bc%9f-kjLVLYjAQY\">\u4ec0\u9ebc\u662f\u6279\u91cf\u64cd\u4f5c\uff1f<\/h2>\n<p>\u6279\u91cf\u64cd\u4f5c\u662f\u6307\u5728\u4e00\u6b21\u8acb\u6c42\u4e2d\u57f7\u884c\u591a\u500b\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u80fd\u529b\u3002\u9019\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u662f\u63d2\u5165\u3001\u66f4\u65b0\u6216\u522a\u9664\u3002\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\uff0c\u5b83\u80fd\u6e1b\u5c11\u8207\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/p>\n<h2 id=\"%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c%e7%9a%84%e9%a1%9e%e5%9e%8b-kjLVLYjAQY\">\u6279\u91cf\u64cd\u4f5c\u7684\u985e\u578b<\/h2>\n<ul>\n<li><strong>\u6279\u91cf\u63d2\u5165\uff08Bulk Insert\uff09<\/strong>: \u5c07\u591a\u689d\u8a18\u9304\u540c\u6642\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002<\/li>\n<li><strong>\u6279\u91cf\u66f4\u65b0\uff08Bulk Update\uff09<\/strong>: \u540c\u6642\u66f4\u65b0\u591a\u689d\u8a18\u9304\uff0c\u6839\u64da\u6307\u5b9a\u7684\u689d\u4ef6\u9032\u884c\u4fee\u6539\u3002<\/li>\n<li><strong>\u6279\u91cf\u522a\u9664\uff08Bulk Delete\uff09<\/strong>: \u6839\u64da\u689d\u4ef6\u4e00\u6b21\u6027\u522a\u9664\u591a\u689d\u8a18\u9304\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9f%b7%e8%a1%8c%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c-kjLVLYjAQY\">\u5982\u4f55\u57f7\u884c\u6279\u91cf\u64cd\u4f5c<\/h2>\n<p>\u5728MongoDB\u4e2d\uff0c\u6279\u91cf\u64cd\u4f5c\u901a\u5e38\u4f7f\u7528Bulk API\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e7%a4%ba%e4%be%8b-kjLVLYjAQY\">\u6279\u91cf\u63d2\u5165\u793a\u4f8b<\/h3>\n<pre><code>\nconst { MongoClient } = require('mongodb');\n\nasync function run() {\n    const client = new MongoClient('mongodb:\/\/localhost:27017');\n    await client.connect();\n    const database = client.db('testdb');\n    const collection = database.collection('testcollection');\n\n    const bulkOp = collection.initializeUnorderedBulkOp();\n    bulkOp.insert({ name: 'Alice', age: 25 });\n    bulkOp.insert({ name: 'Bob', age: 30 });\n    await bulkOp.execute();\n    console.log('\u6279\u91cf\u63d2\u5165\u5b8c\u6210');\n    await client.close();\n}\n\nrun().catch(console.dir);\n<\/code><\/pre>\n<h3 id=\"%e6%89%b9%e9%87%8f%e6%9b%b4%e6%96%b0%e7%a4%ba%e4%be%8b-kjLVLYjAQY\">\u6279\u91cf\u66f4\u65b0\u793a\u4f8b<\/h3>\n<pre><code>\nconst { MongoClient } = require('mongodb');\n\nasync function run() {\n    const client = new MongoClient('mongodb:\/\/localhost:27017');\n    await client.connect();\n    const database = client.db('testdb');\n    const collection = database.collection('testcollection');\n\n    const bulkOp = collection.initializeUnorderedBulkOp();\n    bulkOp.find({ name: 'Alice' }).updateOne({ $set: { age: 26 } });\n    bulkOp.find({ name: 'Bob' }).updateOne({ $set: { age: 31 } });\n    await bulkOp.execute();\n    console.log('\u6279\u91cf\u66f4\u65b0\u5b8c\u6210');\n    await client.close();\n}\n\nrun().catch(console.dir);\n<\/code><\/pre>\n<h3 id=\"%e6%89%b9%e9%87%8f%e5%88%aa%e9%99%a4%e7%a4%ba%e4%be%8b-kjLVLYjAQY\">\u6279\u91cf\u522a\u9664\u793a\u4f8b<\/h3>\n<pre><code>\nconst { MongoClient } = require('mongodb');\n\nasync function run() {\n    const client = new MongoClient('mongodb:\/\/localhost:27017');\n    await client.connect();\n    const database = client.db('testdb');\n    const collection = database.collection('testcollection');\n\n    const bulkOp = collection.initializeUnorderedBulkOp();\n    bulkOp.find({ age: { $lt: 30 } }).deleteMany();\n    await bulkOp.execute();\n    console.log('\u6279\u91cf\u522a\u9664\u5b8c\u6210');\n    await client.close();\n}\n\nrun().catch(console.dir);\n<\/code><\/pre>\n<h2 id=\"%e6%89%b9%e9%87%8f%e6%93%8d%e4%bd%9c%e7%9a%84%e5%84%aa%e5%8b%a2-kjLVLYjAQY\">\u6279\u91cf\u64cd\u4f5c\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347<\/strong>: \u6e1b\u5c11\u4e86\u591a\u6b21\u8acb\u6c42\u7684\u958b\u92b7\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/li>\n<li><strong>\u539f\u5b50\u6027<\/strong>: \u6279\u91cf\u64cd\u4f5c\u53ef\u4ee5\u78ba\u4fdd\u6240\u6709\u64cd\u4f5c\u8981\u9ebc\u5168\u90e8\u6210\u529f\uff0c\u8981\u9ebc\u5168\u90e8\u5931\u6557\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u4e00\u81f4\u6027\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc<\/strong>: \u901a\u904e\u6279\u91cf\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6e1b\u5c11\u4ee3\u78bc\u7684\u8907\u96dc\u6027\uff0c\u8b93\u958b\u767c\u8005\u66f4\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-kjLVLYjAQY\">\u7e3d\u7d50<\/h2>\n<p>MongoDB\u7684\u6279\u91cf\u64cd\u4f5c\u529f\u80fd\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u65b9\u5f0f\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u7121\u8ad6\u662f\u6279\u91cf\u63d2\u5165\u3001\u66f4\u65b0\u9084\u662f\u522a\u9664\uff0c\u9019\u4e9b\u64cd\u4f5c\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u3002\u5728\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u6642\uff0c\u8003\u616e\u5230\u6279\u91cf\u64cd\u4f5c\u7684\u512a\u52e2\uff0cMongoDB\u7121\u7591\u662f\u4e00\u500b\u503c\u5f97\u63a8\u85a6\u7684\u9078\u64c7\u3002\u5982\u679c\u60a8\u5c0d\u65bc<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u6216<code><a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a><\/code>\u6709\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MongoDB\u4e2d\u7684\u6279\u91cf\u64cd\u4f5c\u8853\u8a9e\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u591a\u6587\u6a94\u64cd\u4f5c\uff0c\u512a\u5316\u6027\u80fd\u8207\u8cc7\u6e90\u4f7f\u7528\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-63774","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63774","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=63774"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63774\/revisions"}],"predecessor-version":[{"id":63775,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63774\/revisions\/63775"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=63774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=63774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=63774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}