{"id":2445,"date":"2025-03-07T22:36:36","date_gmt":"2025-03-07T14:36:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2445"},"modified":"2025-03-07T22:41:21","modified_gmt":"2025-03-07T14:41:21","slug":"mongodb-%e6%9d%a1%e4%bb%b6%e6%93%8d%e4%bd%9c%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/07\/mongodb-%e6%9d%a1%e4%bb%b6%e6%93%8d%e4%bd%9c%e7%ac%a6\/","title":{"rendered":"MongoDB \u6761\u4ef6\u64cd\u4f5c\u7b26"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">MongoDB \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684<strong>\u6761\u4ef6\u64cd\u4f5c\u7b26<\/strong>\uff0c\u7528\u4e8e\u7b5b\u9009\u6570\u636e\u5e76\u6267\u884c\u4e0d\u540c\u7684\u67e5\u8be2\u3002\u4e3b\u8981\u7684\u6761\u4ef6\u64cd\u4f5c\u7b26\u5982\u4e0b\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u6bd4\u8f83\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$eq<\/code><\/td><td>\u7b49\u4e8e\uff08equal\uff09<\/td><\/tr><tr><td><code>$ne<\/code><\/td><td>\u4e0d\u7b49\u4e8e\uff08not equal\uff09<\/td><\/tr><tr><td><code>$gt<\/code><\/td><td>\u5927\u4e8e\uff08greater than\uff09<\/td><\/tr><tr><td><code>$gte<\/code><\/td><td>\u5927\u4e8e\u7b49\u4e8e\uff08greater than or equal\uff09<\/td><\/tr><tr><td><code>$lt<\/code><\/td><td>\u5c0f\u4e8e\uff08less than\uff09<\/td><\/tr><tr><td><code>$lte<\/code><\/td><td>\u5c0f\u4e8e\u7b49\u4e8e\uff08less than or equal\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u7b49\u4e8e 100 \u7684\u5546\u54c1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $eq: 100 } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u4e0d\u7b49\u4e8e 100 \u7684\u5546\u54c1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $ne: 100 } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u5927\u4e8e 50 \u7684\u5546\u54c1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $gt: 50 } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u5c0f\u4e8e\u7b49\u4e8e 200 \u7684\u5546\u54c1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $lte: 200 } })<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u903b\u8f91\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$and<\/code><\/td><td>\u903b\u8f91 <strong>AND<\/strong>\uff08\u591a\u4e2a\u6761\u4ef6\u90fd\u6ee1\u8db3\uff09<\/td><\/tr><tr><td><code>$or<\/code><\/td><td>\u903b\u8f91 <strong>OR<\/strong>\uff08\u4efb\u610f\u4e00\u4e2a\u6761\u4ef6\u6ee1\u8db3\uff09<\/td><\/tr><tr><td><code>$not<\/code><\/td><td>\u903b\u8f91 <strong>NOT<\/strong>\uff08\u53d6\u53cd\uff09<\/td><\/tr><tr><td><code>$nor<\/code><\/td><td>\u903b\u8f91 <strong>NOR<\/strong>\uff08\u6240\u6709\u6761\u4ef6\u90fd\u4e0d\u6ee1\u8db3\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u5927\u4e8e 50 \u4e14 <code>category<\/code> \u4e3a &#8220;electronics&#8221;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ \n  $and: &#91;\n    { price: { $gt: 50 } },\n    { category: \"electronics\" }\n  ] \n})<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u5c0f\u4e8e 50 \u6216 <code>category<\/code> \u4e3a &#8220;books&#8221;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ \n  $or: &#91;\n    { price: { $lt: 50 } },\n    { category: \"books\" }\n  ] \n})<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> <strong>\u4e0d\u662f<\/strong> 100<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $not: { $eq: 100 } } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u65e2\u4e0d\u5c0f\u4e8e 50 \u4e5f\u4e0d\u5927\u4e8e 200<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ \n  $nor: &#91;\n    { price: { $lt: 50 } },\n    { price: { $gt: 200 } }\n  ] \n})<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5143\u7d20\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$exists<\/code><\/td><td>\u5b57\u6bb5\u662f\u5426\u5b58\u5728<\/td><\/tr><tr><td><code>$type<\/code><\/td><td>\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>discount<\/code> \u5b57\u6bb5\u5b58\u5728\u7684\u5546\u54c1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ discount: { $exists: true } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u5b57\u6bb5\u7c7b\u578b\u662f <code>double<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $type: \"double\" } })<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u6570\u7ec4\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$all<\/code><\/td><td>\u6570\u7ec4\u5b57\u6bb5\u5fc5\u987b\u5305\u542b\u6307\u5b9a\u6240\u6709\u503c<\/td><\/tr><tr><td><code>$size<\/code><\/td><td>\u6570\u7ec4\u5b57\u6bb5\u7684\u957f\u5ea6<\/td><\/tr><tr><td><code>$elemMatch<\/code><\/td><td>\u6570\u7ec4\u5b57\u6bb5\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>tags<\/code> \u6570\u7ec4\u5305\u542b <code>\"electronics\"<\/code> \u548c <code>\"sale\"<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ tags: { $all: &#91;\"electronics\", \"sale\"] } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>tags<\/code> \u6570\u7ec4\u957f\u5ea6\u4e3a 3<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ tags: { $size: 3 } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>reviews<\/code> \u6570\u7ec4\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u8bc4\u5206\u5927\u4e8e 4 \u5e76\u4e14 <code>user<\/code> \u4e0d\u662f <code>\"guest\"<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ \n  reviews: { \n    $elemMatch: { rating: { $gt: 4 }, user: { $ne: \"guest\" } }\n  } \n})<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$regex<\/code><\/td><td>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/td><\/tr><tr><td><code>$options<\/code><\/td><td>\u914d\u5408 <code>$regex<\/code> \u8fdb\u884c\u5339\u914d\u4fee\u9970\u7b26\uff0c\u5982 <code>i<\/code>\uff08\u5ffd\u7565\u5927\u5c0f\u5199\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>name<\/code> \u5305\u542b <code>\"phone\"<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ name: { $regex: \"phone\" } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>name<\/code> \u4ee5 <code>\"Samsung\"<\/code> \u5f00\u5934\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ name: { $regex: \"^Samsung\", $options: \"i\" } })<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u6a21\u5f0f\u5339\u914d\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$expr<\/code><\/td><td>\u5141\u8bb8\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u805a\u5408\u8868\u8fbe\u5f0f<\/td><\/tr><tr><td><code>$mod<\/code><\/td><td>\u6309\u6a21\u8fd0\u7b97<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>price<\/code> \u662f 5 \u7684\u500d\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ price: { $mod: &#91;5, 0] } })<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>stock<\/code> \u5927\u4e8e <code>min_stock<\/code>\uff08\u57fa\u4e8e\u5b57\u6bb5\u503c\u7684\u8ba1\u7b97\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>db.inventory.find({\n  $expr: { $gt: &#91;\"$stock\", \"$min_stock\"] }\n})<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u6bd4\u7279\u64cd\u4f5c\u7b26<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u7b26<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>$bitsAllSet<\/code><\/td><td>\u6240\u6709\u6307\u5b9a\u4f4d\u5fc5\u987b\u4e3a 1<\/td><\/tr><tr><td><code>$bitsAnySet<\/code><\/td><td>\u4efb\u4f55\u6307\u5b9a\u4f4d\u4e3a 1 \u5373\u5339\u914d<\/td><\/tr><tr><td><code>$bitsAllClear<\/code><\/td><td>\u6240\u6709\u6307\u5b9a\u4f4d\u5fc5\u987b\u4e3a 0<\/td><\/tr><tr><td><code>$bitsAnyClear<\/code><\/td><td>\u4efb\u4f55\u6307\u5b9a\u4f4d\u4e3a 0 \u5373\u5339\u914d<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2 <code>flags<\/code> \u5b57\u6bb5\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\uff0c\u7b2c 1 \u548c 3 \u4f4d\u5fc5\u987b\u4e3a 1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({ flags: { $bitsAllSet: &#91;0, 2] } })<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \u6761\u4ef6\u64cd\u4f5c\u7b26\u63d0\u4f9b\u5f3a\u5927\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u6db5\u76d6\u4e86<strong>\u6bd4\u8f83\u3001\u903b\u8f91\u3001\u6570\u7ec4\u3001\u5143\u7d20\u3001\u6b63\u5219\u3001\u6a21\u5f0f\u5339\u914d\u3001\u6bd4\u7279\u8fd0\u7b97<\/strong>\u7b49\u591a\u79cd\u60c5\u51b5\u3002\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u67e5\u8be2\u80fd\u529b\u548c\u6570\u636e\u7b5b\u9009\u6548\u7387\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u7ed3\u5408\u591a\u4e2a\u64cd\u4f5c\u7b26\u8fdb\u884c\u590d\u6742\u67e5\u8be2\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ \n  $and: &#91;\n    { price: { $gt: 50, $lt: 500 } },\n    { category: { $in: &#91;\"electronics\", \"home\"] } }\n  ]\n})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u8868\u793a\uff1a<strong>\u4ef7\u683c\u5728 50 \u5230 500 \u4e4b\u95f4\uff0c\u5e76\u4e14 <code>category<\/code> \u4e3a <code>\"electronics\"<\/code> \u6216 <code>\"home\"<\/code> \u7684\u5546\u54c1<\/strong>\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>MongoDB \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u6761\u4ef6\u64cd\u4f5c\u7b26\uff0c\u7528\u4e8e\u7b5b\u9009\u6570\u636e\u5e76\u6267\u884c\u4e0d\u540c\u7684\u67e5\u8be2\u3002\u4e3b\u8981\u7684\u6761\u4ef6\u64cd\u4f5c\u7b26\u5982\u4e0b\uff1a 1. \u6bd4\u8f83\u64cd\u4f5c [&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-2445","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2445","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=2445"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2445\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2445\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}