{"id":2441,"date":"2025-03-06T23:27:16","date_gmt":"2025-03-06T15:27:16","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2441"},"modified":"2025-03-06T23:27:16","modified_gmt":"2025-03-06T15:27:16","slug":"mongodb-%e5%88%a0%e9%99%a4%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/06\/mongodb-%e5%88%a0%e9%99%a4%e6%96%87%e6%a1%a3\/","title":{"rendered":"MongoDB \u5220\u9664\u6587\u6863"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 MongoDB \u4e2d\uff0c\u5220\u9664\u6587\u6863\u662f\u79fb\u9664\u6570\u636e\u5e93\u4e2d\u4e0d\u518d\u9700\u8981\u7684\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 MongoDB Shell \u6216\u8005\u901a\u8fc7\u7f16\u7a0b\u8bed\u8a00\u7684 MongoDB \u9a71\u52a8\u7a0b\u5e8f\u6765\u5220\u9664\u6587\u6863\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 Mongo Shell \u5220\u9664\u6587\u6863<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>deleteOne()<\/code> \u6216 <code>deleteMany()<\/code> \u65b9\u6cd5\u6765\u5220\u9664\u6587\u6863\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>deleteOne()<\/code><\/strong>\uff1a\u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u7b2c\u4e00\u4e2a\u6587\u6863\u3002<\/li>\n\n\n\n<li><strong><code>deleteMany()<\/code><\/strong>\uff1a\u5220\u9664\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6b65\u9aa4\uff1a<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8fde\u63a5\u5230 MongoDB \u5b9e\u4f8b\uff1a<\/strong><br>\u6253\u5f00\u7ec8\u7aef\u5e76\u8fde\u63a5\u5230 MongoDB \u5b9e\u4f8b\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   mongo<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u6570\u636e\u5e93\uff1a<\/strong><br>\u4f7f\u7528 <code>use<\/code> \u547d\u4ee4\u5207\u6362\u5230\u4f60\u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   use myDatabase<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u5220\u9664\u5355\u4e2a\u6587\u6863\uff1a<\/strong><br>\u4f7f\u7528 <code>deleteOne()<\/code> \u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u7b2c\u4e00\u4e2a\u6587\u6863\u3002\u4f8b\u5982\uff0c\u5220\u9664 <code>name<\/code> \u4e3a &#8220;Alice&#8221; \u7684\u6587\u6863\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   db.users.deleteOne({ name: \"Alice\" })<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u5220\u9664\u591a\u4e2a\u6587\u6863\uff1a<\/strong><br>\u4f7f\u7528 <code>deleteMany()<\/code> \u5220\u9664\u6240\u6709\u5339\u914d\u6761\u4ef6\u7684\u6587\u6863\u3002\u4f8b\u5982\uff0c\u5220\u9664\u6240\u6709 <code>age<\/code> \u5927\u4e8e 30 \u7684\u6587\u6863\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   db.users.deleteMany({ age: { $gt: 30 } })<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>\u786e\u8ba4\u5220\u9664\uff1a<\/strong><br>\u4f7f\u7528 <code>find()<\/code> \u67e5\u8be2\u5269\u4f59\u7684\u6570\u636e\uff0c\u786e\u8ba4\u6587\u6863\u662f\u5426\u5df2\u5220\u9664\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   db.users.find()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 MongoDB \u9a71\u52a8\u7a0b\u5e8f\u5220\u9664\u6587\u6863<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u8bed\u8a00\u7684 MongoDB \u9a71\u52a8\u7a0b\u5e8f\u6765\u5220\u9664\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7 <strong>Node.js<\/strong> \u548c <strong>Python<\/strong> \u6765\u5220\u9664\u6587\u6863\u7684\u793a\u4f8b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Node.js \u793a\u4f8b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u5b89\u88c5 MongoDB Node.js \u9a71\u52a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install mongodb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5220\u9664\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { MongoClient } = require('mongodb');\n\n\/\/ MongoDB URI\nconst uri = \"mongodb:\/\/localhost:27017\";\n\n\/\/ \u521b\u5efa MongoDB \u5ba2\u6237\u7aef\nconst client = new MongoClient(uri);\n\nasync function deleteDocument() {\n  try {\n    \/\/ \u8fde\u63a5\u5230 MongoDB\n    await client.connect();\n    console.log(\"\u6210\u529f\u8fde\u63a5\u5230 MongoDB\");\n\n    \/\/ \u83b7\u53d6\u6570\u636e\u5e93\n    const db = client.db(\"myDatabase\");\n\n    \/\/ \u5220\u9664\u5355\u4e2a\u6587\u6863\n    const result = await db.collection(\"users\").deleteOne({ name: \"Alice\" });\n    console.log(\"\u5220\u9664\u7684\u6587\u6863\u6570\u91cf:\", result.deletedCount);\n\n    \/\/ \u5220\u9664\u591a\u4e2a\u6587\u6863\n    const resultMany = await db.collection(\"users\").deleteMany({ age: { $gt: 30 } });\n    console.log(\"\u5220\u9664\u7684\u6587\u6863\u6570\u91cf:\", resultMany.deletedCount);\n\n  } finally {\n    await client.close();\n  }\n}\n\ndeleteDocument().catch(console.error);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Python \u793a\u4f8b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u5b89\u88c5 <code>pymongo<\/code> \u9a71\u52a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pymongo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5220\u9664\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pymongo import MongoClient\n\n# MongoDB URI\nuri = \"mongodb:\/\/localhost:27017\"\n\n# \u521b\u5efa MongoDB \u5ba2\u6237\u7aef\nclient = MongoClient(uri)\n\n# \u83b7\u53d6\u6570\u636e\u5e93\ndb = client&#91;\"myDatabase\"]\n\n# \u5220\u9664\u5355\u4e2a\u6587\u6863\nresult = db&#91;\"users\"].delete_one({ \"name\": \"Alice\" })\nprint(\"\u5220\u9664\u7684\u6587\u6863\u6570\u91cf:\", result.deleted_count)\n\n# \u5220\u9664\u591a\u4e2a\u6587\u6863\nresult_many = db&#91;\"users\"].delete_many({ \"age\": { \"$gt\": 30 } })\nprint(\"\u5220\u9664\u7684\u6587\u6863\u6570\u91cf:\", result_many.deleted_count)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u5220\u9664\u6587\u6863\u64cd\u4f5c\u7b26<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 MongoDB \u4e2d\uff0c\u5220\u9664\u6587\u6863\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u4e2a\u67e5\u8be2\u6761\u4ef6\u6765\u6307\u5b9a\u8981\u5220\u9664\u7684\u6587\u6863\u3002\u4e0e\u66f4\u65b0\u64cd\u4f5c\u7b26\u7c7b\u4f3c\uff0c\u5220\u9664\u64cd\u4f5c\u5e76\u6ca1\u6709\u7279\u6b8a\u7684\u64cd\u4f5c\u7b26\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u6761\u4ef6\u975e\u5e38\u7075\u6d3b\u5730\u63a7\u5236\u5220\u9664\u7684\u6587\u6863\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>{ age: { $gt: 30 } }<\/code><\/strong>\uff1a\u5220\u9664 <code>age<\/code> \u5927\u4e8e 30 \u7684\u6240\u6709\u6587\u6863\u3002<\/li>\n\n\n\n<li><strong><code>{ city: \"New York\" }<\/code><\/strong>\uff1a\u5220\u9664 <code>city<\/code> \u4e3a &#8220;New York&#8221; \u7684\u6240\u6709\u6587\u6863\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5220\u9664\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u6863\u3002\u4f8b\u5982\uff0c\u5220\u9664\u6240\u6709 <code>name<\/code> \u4ee5 &#8220;A&#8221; \u5f00\u5934\u7684\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.deleteMany({ name: \/^A\/ })<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5220\u9664\u6587\u6863\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u53ef\u6062\u590d<\/strong>\uff1a\u5220\u9664\u64cd\u4f5c\u4f1a\u6c38\u4e45\u5220\u9664\u6587\u6863\uff0c\u65e0\u6cd5\u6062\u590d\uff0c\u56e0\u6b64\u8bf7\u8c28\u614e\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u6279\u91cf\u5220\u9664<\/strong>\uff1a\u4f7f\u7528 <code>deleteMany()<\/code> \u5220\u9664\u591a\u4e2a\u6587\u6863\u65f6\uff0c\u8981\u5c0f\u5fc3\u67e5\u8be2\u6761\u4ef6\uff0c\u4ee5\u514d\u5220\u9664\u4e86\u4e0d\u9700\u8981\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u5220\u9664\u7ed3\u679c<\/strong>\uff1a<code>deleteOne()<\/code> \u548c <code>deleteMany()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u5bf9\u8c61\u5305\u542b <code>deletedCount<\/code>\uff08\u5df2\u5220\u9664\u6587\u6863\u7684\u6570\u91cf\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5220\u9664\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u6279\u91cf\u5220\u9664\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u636e\u5e93\u6027\u80fd\u3002\u5728\u5220\u9664\u5927\u91cf\u6570\u636e\u65f6\uff0c\u8003\u8651\u4f7f\u7528\u5206\u9875\u6216\u5206\u6279\u5220\u9664\u7b56\u7565\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 MongoDB \u4e2d\uff0c\u5220\u9664\u6587\u6863\u662f\u901a\u8fc7 <code>deleteOne()<\/code> \u6216 <code>deleteMany()<\/code> \u6765\u5b9e\u73b0\u7684\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u5355\u4e2a\u6216\u591a\u4e2a\u6587\u6863\uff0c\u4f7f\u7528\u7075\u6d3b\u7684\u67e5\u8be2\u6761\u4ef6\u6765\u7cbe\u786e\u63a7\u5236\u8981\u5220\u9664\u7684\u6570\u636e\u3002\u5728\u6267\u884c\u5220\u9664\u64cd\u4f5c\u65f6\uff0c\u52a1\u5fc5\u5c0f\u5fc3\uff0c\u786e\u4fdd\u5220\u9664\u7684\u6570\u636e\u662f\u6b63\u786e\u7684\uff0c\u56e0\u4e3a\u5220\u9664\u662f\u4e0d\u53ef\u6062\u590d\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MongoDB \u4e2d\uff0c\u5220\u9664\u6587\u6863\u662f\u79fb\u9664\u6570\u636e\u5e93\u4e2d\u4e0d\u518d\u9700\u8981\u7684\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 MongoDB Shell \u6216 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-2441","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2441","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=2441"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2441\/revisions"}],"predecessor-version":[{"id":2442,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2441\/revisions\/2442"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}