{"id":5366,"date":"2024-11-14T10:59:55","date_gmt":"2024-11-14T02:59:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5366\/"},"modified":"2024-11-14T10:59:55","modified_gmt":"2024-11-14T02:59:55","slug":"mongodb%e6%80%8e%e4%b9%88%e6%89%b9%e9%87%8f%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5366\/","title":{"rendered":"mongodb\u600e\u4e48\u6279\u91cf\u5220\u9664"},"content":{"rendered":"<blockquote><p>\n  mongodb \u4e2d\u6279\u91cf\u5220\u9664\u6587\u6863\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a1. $in \u64cd\u4f5c\u7b26\u6307\u5b9a\u8981\u5220\u9664\u7684\u6587\u6863\u5217\u8868\uff1b2. \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\uff1b3. $exists \u64cd\u4f5c\u7b26\u5220\u9664\u5177\u6709\u6307\u5b9a\u5b57\u6bb5\u7684\u6587\u6863\uff1b4. find() \u548c remove() \u65b9\u6cd5\u5148\u83b7\u53d6\u518d\u5220\u9664\u6587\u6863\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u65e0\u6cd5\u4f7f\u7528\u4e8b\u52a1\uff0c\u5e76\u53ef\u80fd\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u6587\u6863\uff0c\u56e0\u6b64\u4f7f\u7528\u65f6\u9700\u8c28\u614e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052802062148711.jpg\" class=\"aligncenter\" title=\"mongodb\u600e\u4e48\u6279\u91cf\u5220\u9664\u63d2\u56fe\" alt=\"mongodb\u600e\u4e48\u6279\u91cf\u5220\u9664\u63d2\u56fe\" \/><\/p>\n<p><strong>MongoDB \u4e2d\u6279\u91cf\u5220\u9664\u6587\u6863<\/strong><\/p>\n<p>\u5728 MongoDB \u4e2d\u6279\u91cf\u5220\u9664\u6587\u6863\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 $in \u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528 $in \u64cd\u4f5c\u7b26\u6765\u6307\u5b9a\u8981\u5220\u9664\u7684\u6587\u6863\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>db.collection.deleteMany({ _id: { $in: [ObjectId(\"5e878f3309791e8153091cb3\"), ObjectId(\"5e878f4209791e8153091cb4\")] }})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>db.collection.deleteMany({ name: \/abc\/ })<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 $exists \u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528 $exists \u64cd\u4f5c\u7b26\u6765\u5220\u9664\u5177\u6709\u6307\u5b9a\u5b57\u6bb5\u7684\u6587\u6863\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>db.collection.deleteMany({ field: { $exists: true } })<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 find() \u548c remove() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5148\u4f7f\u7528 find() \u65b9\u6cd5\u83b7\u53d6\u8981\u5220\u9664\u7684\u6587\u6863\uff0c\u7136\u540e\u518d\u4f7f\u7528 remove() \u65b9\u6cd5\u5220\u9664\u5b83\u4eec\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>db.collection.find({ name: \"John\" }).remove()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6279\u91cf\u5220\u9664\u64cd\u4f5c\u65e0\u6cd5\u4f7f\u7528\u4e8b\u52a1\u3002<\/li>\n<li>\u6279\u91cf\u5220\u9664\u64cd\u4f5c\u4f1a\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u6587\u6863\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u8981\u5c0f\u5fc3\u3002<\/li>\n<li>\u4f7f\u7528 $in \u64cd\u4f5c\u7b26\u65f6\uff0cObjectId \u5fc5\u987b\u4f7f\u7528 ObjectId() \u6784\u9020\u51fd\u6570\u8f6c\u6362\u4e3a MongoDB ObjectId \u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u6279\u91cf\u5220\u9664\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mongodb \u4e2d\u6279\u91cf\u5220\u9664\u6587\u6863\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a1. $in \u64cd\u4f5c\u7b26\u6307\u5b9a\u8981\u5220\u9664\u7684\u6587\u6863\u5217\u8868\uff1b2. \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\uff1b3. $exists \u64cd\u4f5c\u7b26\u5220\u9664\u5177\u6709\u6307\u5b9a\u5b57\u6bb5\u7684\u6587\u6863\uff1b4. find() \u548c remove() \u65b9\u6cd5\u5148\u83b7\u53d6\u518d\u5220\u9664\u6587\u6863\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u65e0\u6cd5\u4f7f\u7528\u4e8b\u52a1\uff0c\u5e76\u53ef\u80fd\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u6587\u6863\uff0c\u56e0\u6b64\u4f7f\u7528\u65f6\u9700\u8c28\u614e\u3002 MongoDB \u4e2d\u6279\u91cf\u5220\u9664\u6587\u6863 \u5728 MongoDB \u4e2d\u6279\u91cf\u5220\u9664\u6587\u6863\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 $in \u64cd\u4f5c\u7b26 \u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528 $in \u64cd\u4f5c\u7b26\u6765\u6307\u5b9a\u8981\u5220\u9664\u7684\u6587\u6863\u5217\u8868\u3002\u4f8b\u5982\uff1a db.collection.deleteMany({ _id: { $in: [ObjectId(&#8220;5e878f3309791e8153091cb3&#8221;), ObjectId(&#8220;5e878f4209791e8153091cb4&#8221;)] }}) \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f \u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002\u4f8b\u5982\uff1a db.collection.deleteMany({ name: \/abc\/ }) \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 $exists \u64cd\u4f5c\u7b26 \u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528 $exists \u64cd\u4f5c\u7b26\u6765\u5220\u9664\u5177\u6709\u6307\u5b9a\u5b57\u6bb5\u7684\u6587\u6863\u3002\u4f8b\u5982\uff1a db.collection.deleteMany({ field: { $exists: true } }) \u767b\u5f55\u540e\u590d\u5236 4. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5366","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5366"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5366\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}