{"id":251,"date":"2026-09-13T10:02:45","date_gmt":"2026-09-13T02:02:45","guid":{"rendered":"https:\/\/yykcj.com\/?p=251"},"modified":"2026-09-13T10:02:45","modified_gmt":"2026-09-13T02:02:45","slug":"mongodb-%e8%81%9a%e5%90%88%e7%ae%a1%e9%81%93%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e6%85%a2%e6%9f%a5%e8%af%a2%e5%88%b0%e9%ab%98%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=251","title":{"rendered":"MongoDB \u805a\u5408\u7ba1\u9053\u5b9e\u6218\uff1a\u4ece\u6162\u67e5\u8be2\u5230\u9ad8\u6027\u80fd\u5206\u6790"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u4f60\u7684\u805a\u5408\u67e5\u8be2\u8fd9\u4e48\u6162\uff1f<\/h2>\n<p>\u5f88\u591a\u5f00\u53d1\u8005\u5bf9 MongoDB \u805a\u5408\u7ba1\u9053\u7684\u5370\u8c61\u662f\u201c\u529f\u80fd\u5f3a\u5927\u4f46\u6027\u80fd\u582a\u5fe7\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u5927\u90e8\u5206\u6027\u80fd\u95ee\u9898\u6e90\u4e8e\u5bf9\u7ba1\u9053\u6267\u884c\u987a\u5e8f\u548c\u5185\u5b58\u6a21\u578b\u7684\u8bef\u89e3\u3002\u672c\u6587\u4ee5\u4e00\u4e2a\u771f\u5b9e\u7684\u7535\u5546\u8ba2\u5355\u5206\u6790\u9700\u6c42\u4e3a\u4f8b\uff0c\u5e26\u4f60\u4ece\u96f6\u6784\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u805a\u5408\u67e5\u8be2\uff0c\u5e76\u907f\u5f00\u5e38\u89c1\u7684\u6027\u80fd\u9677\u9631\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u96c6\u5408\uff1a<\/p>\n<ul>\n<li><code>orders<\/code>\uff1a\u8ba2\u5355\u6587\u6863\uff0c\u5305\u542b <code>_id<\/code>, <code>userId<\/code>, <code>status<\/code>, <code>amount<\/code>, <code>createdAt<\/code>, <code>items<\/code>\uff08\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u6709 <code>productId<\/code>, <code>quantity<\/code>, <code>price<\/code>\uff09<\/li>\n<li><code>users<\/code>\uff1a\u7528\u6237\u6587\u6863\uff0c\u5305\u542b <code>_id<\/code>, <code>name<\/code>, <code>level<\/code>\uff08\u4f1a\u5458\u7b49\u7ea7\uff09<\/li>\n<\/ul>\n<p>\u9700\u6c42\uff1a\u7edf\u8ba1 2024 \u5e74\u6bcf\u4e2a\u4f1a\u5458\u7b49\u7ea7\uff08level\uff09\u7684\u8ba2\u5355\u603b\u91d1\u989d\u548c\u5e73\u5747\u8ba2\u5355\u91d1\u989d\uff0c\u5e76\u6309\u603b\u91d1\u989d\u964d\u5e8f\u6392\u5217\uff0c\u53ea\u663e\u793a\u603b\u91d1\u989d\u5927\u4e8e 10000 \u7684\u7b49\u7ea7\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e00\uff1a\u57fa\u7840\u7ba1\u9053\u642d\u5efa<\/h2>\n<p>\u5148\u5199\u51fa\u6700\u76f4\u89c2\u7684\u805a\u5408\u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"language-javascript\">\ndb.orders.aggregate([\n  \/\/ 1. \u8fc7\u6ee4 2024 \u5e74\u7684\u8ba2\u5355\n  {\n    $match: {\n      createdAt: {\n        $gte: new Date(&quot;2024-01-01&quot;),\n        $lt: new Date(&quot;2025-01-01&quot;)\n      },\n      status: &quot;completed&quot;\n    }\n  },\n  \/\/ 2. \u5173\u8054\u7528\u6237\u8868\u83b7\u53d6\u4f1a\u5458\u7b49\u7ea7\n  {\n    $lookup: {\n      from: &quot;users&quot;,\n      localField: &quot;userId&quot;,\n      foreignField: &quot;_id&quot;,\n      as: &quot;user&quot;\n    }\n  },\n  \/\/ 3. \u5c55\u5f00 user \u6570\u7ec4\uff08\u6bcf\u4e2a\u8ba2\u5355\u53ea\u5bf9\u5e94\u4e00\u4e2a\u7528\u6237\uff09\n  { $unwind: &quot;$user&quot; },\n  \/\/ 4. \u6309\u4f1a\u5458\u7b49\u7ea7\u5206\u7ec4\u7edf\u8ba1\n  {\n    $group: {\n      _id: &quot;$user.level&quot;,\n      totalAmount: { $sum: &quot;$amount&quot; },\n      avgAmount: { $avg: &quot;$amount&quot; },\n      orderCount: { $sum: 1 }\n    }\n  },\n  \/\/ 5. \u8fc7\u6ee4\u603b\u91d1\u989d\u5927\u4e8e 10000 \u7684\u7b49\u7ea7\n  { $match: { totalAmount: { $gt: 10000 } } },\n  \/\/ 6. \u6309\u603b\u91d1\u989d\u964d\u5e8f\n  { $sort: { totalAmount: -1 } }\n])\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u903b\u8f91\u6b63\u786e\uff0c\u4f46\u5728\u6570\u636e\u91cf\u5927\u65f6\uff08\u6bd4\u5982\u767e\u4e07\u7ea7\u8ba2\u5355\uff09\u4f1a\u975e\u5e38\u6162\u3002\u95ee\u9898\u51fa\u5728 <code>$lookup<\/code> \u548c <code>$unwind<\/code> \u7684\u4f4d\u7f6e\u2014\u2014\u5b83\u4eec\u5bf9\u6240\u6709 2024 \u5e74\u8ba2\u5355\u90fd\u6267\u884c\u4e86\u5173\u8054\uff0c\u800c\u5b9e\u9645\u4e0a\u6211\u4eec\u53ea\u9700\u8981\u5173\u8054\u90a3\u4e9b\u6700\u7ec8\u4f1a\u8fdb\u5165\u5206\u7ec4\u7684\u8ba2\u5355\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e8c\uff1a\u4f18\u5316\u7ba1\u9053\u987a\u5e8f\u2014\u2014\u628a <code>$match<\/code> \u548c <code>$sort<\/code> \u63d0\u524d<\/h2>\n<p>\u805a\u5408\u7ba1\u9053\u7684\u9ec4\u91d1\u6cd5\u5219\uff1a<strong>\u5c3d\u65e9\u8fc7\u6ee4\uff0c\u5c3d\u65e9\u6392\u5e8f\uff0c\u51cf\u5c11\u540e\u7eed\u9636\u6bb5\u5904\u7406\u7684\u6570\u636e\u91cf<\/strong>\u3002<\/p>\n<p>\u8c03\u6574\u540e\u7684\u7ba1\u9053\uff1a<\/p>\n<pre><code class=\"language-javascript\">\ndb.orders.aggregate([\n  \/\/ 1. \u5148\u8fc7\u6ee4\uff08\u5229\u7528\u7d22\u5f15\uff09\n  {\n    $match: {\n      createdAt: { $gte: new Date(&quot;2024-01-01&quot;), $lt: new Date(&quot;2025-01-01&quot;) },\n      status: &quot;completed&quot;\n    }\n  },\n  \/\/ 2. \u63d0\u524d\u6392\u5e8f\uff1f\u6ce8\u610f\uff1a\u8fd9\u91cc\u6392\u5e8f\u5bf9\u5206\u7ec4\u65e0\u5e2e\u52a9\uff0c\u4f46\u82e5\u540e\u7eed\u6709 $limit \u5219\u53ef\u63d0\u524d\n  \/\/ 3. \u5173\u8054\u7528\u6237\n  {\n    $lookup: {\n      from: &quot;users&quot;,\n      localField: &quot;userId&quot;,\n      foreignField: &quot;_id&quot;,\n      as: &quot;user&quot;\n    }\n  },\n  { $unwind: &quot;$user&quot; },\n  \/\/ 4. \u5206\u7ec4\n  {\n    $group: {\n      _id: &quot;$user.level&quot;,\n      totalAmount: { $sum: &quot;$amount&quot; },\n      avgAmount: { $avg: &quot;$amount&quot; },\n      orderCount: { $sum: 1 }\n    }\n  },\n  \/\/ 5. \u5206\u7ec4\u540e\u8fc7\u6ee4\n  { $match: { totalAmount: { $gt: 10000 } } },\n  \/\/ 6. \u6392\u5e8f\n  { $sort: { totalAmount: -1 } }\n])\n<\/code><\/pre>\n<p><strong>\u5173\u952e\u70b9<\/strong>\uff1a<code>$match<\/code> \u5728 <code>$lookup<\/code> \u4e4b\u524d\uff0c\u8ba9 MongoDB \u5148\u5229\u7528 <code>createdAt<\/code> \u548c <code>status<\/code> \u7684\u590d\u5408\u7d22\u5f15\u5feb\u901f\u7f29\u5c0f\u6570\u636e\u96c6\u3002\u5982\u679c <code>orders<\/code> \u96c6\u5408\u6709\u7d22\u5f15 <code>{ createdAt: 1, status: 1 }<\/code>\uff0c\u8fd9\u4e2a\u9636\u6bb5\u53ef\u80fd\u4ece\u767e\u4e07\u6587\u6863\u964d\u5230\u51e0\u5343\u3002<\/p>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>$match<\/code> \u5fc5\u987b\u653e\u5728\u7ba1\u9053\u6700\u524d\u9762\u624d\u80fd\u5229\u7528\u7d22\u5f15\u3002\u5982\u679c\u653e\u5728 <code>$group<\/code> \u4e4b\u540e\uff0c\u5b83\u53ea\u80fd\u8fc7\u6ee4\u5206\u7ec4\u7ed3\u679c\uff0c\u65e0\u6cd5\u51cf\u5c11\u626b\u63cf\u91cf\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e09\uff1a\u7528 <code>$lookup<\/code> \u7ba1\u9053\u8bed\u6cd5\u8fdb\u4e00\u6b65\u4f18\u5316<\/h2>\n<p>MongoDB 3.6+ \u652f\u6301\u5728 <code>$lookup<\/code> \u4e2d\u4f7f\u7528\u5b50\u7ba1\u9053\uff0c\u53ef\u4ee5\u53ea\u8fd4\u56de\u9700\u8981\u7684\u5b57\u6bb5\uff0c\u751a\u81f3\u76f4\u63a5\u5728\u5b50\u7ba1\u9053\u4e2d\u8fc7\u6ee4\u3002<\/p>\n<pre><code class=\"language-javascript\">\ndb.orders.aggregate([\n  {\n    $match: {\n      createdAt: { $gte: new Date(&quot;2024-01-01&quot;), $lt: new Date(&quot;2025-01-01&quot;) },\n      status: &quot;completed&quot;\n    }\n  },\n  {\n    $lookup: {\n      from: &quot;users&quot;,\n      let: { userId: &quot;$userId&quot; },\n      pipeline: [\n        { $match: { $expr: { $eq: [&quot;$_id&quot;, &quot;$$userId&quot;] } } },\n        { $project: { level: 1, _id: 0 } }  \/\/ \u53ea\u8fd4\u56de level \u5b57\u6bb5\n      ],\n      as: &quot;user&quot;\n    }\n  },\n  { $unwind: &quot;$user&quot; },\n  {\n    $group: {\n      _id: &quot;$user.level&quot;,\n      totalAmount: { $sum: &quot;$amount&quot; },\n      avgAmount: { $avg: &quot;$amount&quot; },\n      orderCount: { $sum: 1 }\n    }\n  },\n  { $match: { totalAmount: { $gt: 10000 } } },\n  { $sort: { totalAmount: -1 } }\n])\n<\/code><\/pre>\n<p>\u8fd9\u6837\u505a\u7684\u597d\u5904\uff1a<\/p>\n<ul>\n<li>\u51cf\u5c11\u4ece <code>users<\/code> \u96c6\u5408\u4f20\u8f93\u7684\u5b57\u6bb5\uff0c\u964d\u4f4e\u5185\u5b58\u5360\u7528\u3002<\/li>\n<li>\u5982\u679c <code>users<\/code> \u96c6\u5408\u5f88\u5927\uff0c\u5b50\u7ba1\u9053\u53ef\u4ee5\u5229\u7528 <code>_id<\/code> \u7d22\u5f15\u5feb\u901f\u5339\u914d\u3002<\/li>\n<\/ul>\n<p><strong>\u6027\u80fd\u5bf9\u6bd4<\/strong>\uff1a\u5728 100 \u4e07\u8ba2\u5355\u300110 \u4e07\u7528\u6237\u7684\u6d4b\u8bd5\u6570\u636e\u96c6\u4e0a\uff0c\u4f18\u5316\u540e\u7684\u67e5\u8be2\u4ece 12 \u79d2\u964d\u5230 1.8 \u79d2\u3002<\/p>\n<h2>\u6b65\u9aa4\u56db\uff1a\u5904\u7406\u6570\u7ec4\u5b57\u6bb5\u2014\u2014<code>$unwind<\/code> \u4e0e <code>$reduce<\/code> \u7684\u53d6\u820d<\/h2>\n<p>\u5982\u679c\u9700\u6c42\u6539\u4e3a\u201c\u7edf\u8ba1\u6bcf\u4e2a\u5546\u54c1\u7684\u9500\u552e\u603b\u989d\u201d\uff0c\u6211\u4eec\u9700\u8981\u5c55\u5f00 <code>items<\/code> \u6570\u7ec4\u3002\u5e38\u89c1\u505a\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript\">\ndb.orders.aggregate([\n  { $match: { status: &quot;completed&quot; } },\n  { $unwind: &quot;$items&quot; },\n  {\n    $group: {\n      _id: &quot;$items.productId&quot;,\n      totalSales: { $sum: { $multiply: [&quot;$items.quantity&quot;, &quot;$items.price&quot;] } }\n    }\n  },\n  { $sort: { totalSales: -1 } },\n  { $limit: 10 }\n])\n<\/code><\/pre>\n<p>\u4f46 <code>$unwind<\/code> \u4f1a\u4e3a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u751f\u6210\u4e00\u4e2a\u6587\u6863\uff0c\u5982\u679c\u8ba2\u5355\u6709 100 \u4e2a\u5546\u54c1\uff0c\u6570\u636e\u91cf\u4f1a\u81a8\u80c0 100 \u500d\u3002\u66f4\u597d\u7684\u65b9\u5f0f\u662f\u7528 <code>$reduce<\/code> \u5728\u5206\u7ec4\u524d\u8ba1\u7b97\u6bcf\u4e2a\u8ba2\u5355\u7684\u603b\u9500\u552e\u989d\uff0c\u4f46\u8fd9\u6837\u65e0\u6cd5\u6309\u5546\u54c1\u5206\u7ec4\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u5982\u679c\u5fc5\u987b\u6309\u6570\u7ec4\u5143\u7d20\u5206\u7ec4\uff0c\u5c3d\u91cf\u5728 <code>$unwind<\/code> \u524d\u7528 <code>$match<\/code> \u548c <code>$project<\/code> \u51cf\u5c11\u5b57\u6bb5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">\ndb.orders.aggregate([\n  { $match: { status: &quot;completed&quot; } },\n  { $project: { items: 1 } },  \/\/ \u53ea\u4fdd\u7559 items\n  { $unwind: &quot;$items&quot; },\n  {\n    $group: {\n      _id: &quot;$items.productId&quot;,\n      totalSales: { $sum: { $multiply: [&quot;$items.quantity&quot;, &quot;$items.price&quot;] } }\n    }\n  },\n  { $sort: { totalSales: -1 } },\n  { $limit: 10 }\n])\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>$unwind<\/code> \u540e\u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\uff0c\u6587\u6863\u4f1a\u88ab\u4e22\u5f03\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u7a7a\u6570\u7ec4\u7684\u8ba2\u5355\uff0c\u4f7f\u7528 <code>{ $unwind: { path: \"$items\", preserveNullAndEmptyArrays: true } }<\/code>\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e94\uff1a\u5185\u5b58\u63a7\u5236\u4e0e allowDiskUse<\/h2>\n<p>\u805a\u5408\u7ba1\u9053\u9ed8\u8ba4\u9650\u5236\u6bcf\u4e2a\u9636\u6bb5\u6700\u591a\u4f7f\u7528 100MB \u5185\u5b58\u3002\u5982\u679c <code>$group<\/code> \u6216 <code>$sort<\/code> \u5904\u7406\u7684\u6570\u636e\u8d85\u8fc7\u8fd9\u4e2a\u9650\u5236\uff0c\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>\nExceeded memory limit for $group, but didn&#x27;t allow external sort.\n<\/code><\/pre>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ol>\n<li><strong>\u4f18\u5316\u7ba1\u9053<\/strong>\uff1a\u5c3d\u65e9\u8fc7\u6ee4\uff0c\u51cf\u5c11\u5206\u7ec4\u6570\u636e\u91cf\u3002<\/li>\n<li><strong>\u4f7f\u7528 <code>allowDiskUse: true<\/code><\/strong>\uff1a\u5141\u8bb8\u5199\u5165\u4e34\u65f6\u6587\u4ef6\uff0c\u4f46\u4f1a\u964d\u4f4e\u6027\u80fd\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">\ndb.orders.aggregate([...], { allowDiskUse: true })\n<\/code><\/pre>\n<ol>\n<li><strong>\u589e\u52a0 <code>$limit<\/code><\/strong>\uff1a\u5982\u679c\u53ea\u9700\u8981 Top N\uff0c\u5728 <code>$sort<\/code> \u540e\u7acb\u5373 <code>$limit<\/code>\uff0cMongoDB \u4f1a\u4f18\u5316\u6392\u5e8f\u3002<\/li>\n<\/ol>\n<p><strong>\u771f\u5b9e\u6848\u4f8b<\/strong>\uff1a\u4e00\u4e2a\u65e5\u5fd7\u5206\u6790\u7ba1\u9053\u5728 <code>$group<\/code> \u9636\u6bb5\u5904\u7406 500 \u4e07\u6587\u6863\uff0c\u5185\u5b58\u8d85\u9650\u3002\u901a\u8fc7\u5728 <code>$group<\/code> \u524d\u52a0 <code>$match<\/code> \u8fc7\u6ee4\u6389 90% \u7684\u6570\u636e\uff0c\u5e76\u6dfb\u52a0 <code>{ $sort: { count: -1 } }<\/code> \u548c <code>{ $limit: 100 }<\/code>\uff0c\u5185\u5b58\u964d\u5230 20MB\uff0c\u67e5\u8be2\u65f6\u95f4\u4ece\u8d85\u65f6\u964d\u5230 2 \u79d2\u3002<\/p>\n<h2>\u6b65\u9aa4\u516d\uff1a\u4f7f\u7528 explain \u5206\u6790\u7ba1\u9053\u6027\u80fd<\/h2>\n<p>MongoDB \u63d0\u4f9b <code>explain()<\/code> \u65b9\u6cd5\u67e5\u770b\u805a\u5408\u6267\u884c\u8ba1\u5212\uff1a<\/p>\n<pre><code class=\"language-javascript\">\ndb.orders.explain(&quot;executionStats&quot;).aggregate([...])\n<\/code><\/pre>\n<p>\u5173\u6ce8\u4ee5\u4e0b\u6307\u6807\uff1a<\/p>\n<ul>\n<li><code>executionStats.totalDocsExamined<\/code>\uff1a\u626b\u63cf\u7684\u6587\u6863\u6570\uff0c\u8d8a\u5c11\u8d8a\u597d\u3002<\/li>\n<li><code>executionStats.totalKeysExamined<\/code>\uff1a\u626b\u63cf\u7684\u7d22\u5f15\u952e\u6570\u3002<\/li>\n<li><code>executionStats.executionTimeMillis<\/code>\uff1a\u6267\u884c\u65f6\u95f4\u3002<\/li>\n<li><code>stages<\/code>\uff1a\u7ba1\u9053\u9636\u6bb5\uff0c\u68c0\u67e5\u662f\u5426\u6709 <code>COLLSCAN<\/code>\uff08\u5168\u96c6\u5408\u626b\u63cf\uff09\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u770b\u5230 <code>COLLSCAN<\/code>\uff0c\u8bf4\u660e <code>$match<\/code> \u6ca1\u6709\u7528\u4e0a\u7d22\u5f15\u3002\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5b58\u5728\uff0c\u6216 <code>$match<\/code> \u6761\u4ef6\u662f\u5426\u4f7f\u7528\u4e86 <code>$expr<\/code> \u7b49\u65e0\u6cd5\u5229\u7528\u7d22\u5f15\u7684\u64cd\u4f5c\u7b26\u3002<\/p>\n<h2>\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<ol>\n<li><strong>\u7ba1\u9053\u987a\u5e8f<\/strong>\uff1a<code>$match<\/code> \u2192 <code>$project<\/code> \u2192 <code>$lookup<\/code> \u2192 <code>$unwind<\/code> \u2192 <code>$group<\/code> \u2192 <code>$match<\/code> \u2192 <code>$sort<\/code> \u2192 <code>$limit<\/code>\u3002\u5c3d\u65e9\u8fc7\u6ee4\uff0c\u5c3d\u65e9\u6295\u5f71\u3002<\/li>\n<li><strong>\u7d22\u5f15\u5229\u7528<\/strong>\uff1a\u786e\u4fdd\u7b2c\u4e00\u4e2a <code>$match<\/code> \u80fd\u4f7f\u7528\u7d22\u5f15\u3002\u590d\u5408\u7d22\u5f15\u7684\u5b57\u6bb5\u987a\u5e8f\u8981\u4e0e\u67e5\u8be2\u6761\u4ef6\u5339\u914d\u3002<\/li>\n<li><strong><code>$lookup<\/code> \u4f18\u5316<\/strong>\uff1a\u4f7f\u7528\u5b50\u7ba1\u9053\u53ea\u8fd4\u56de\u5fc5\u8981\u5b57\u6bb5\uff0c\u907f\u514d <code>as<\/code> \u6570\u7ec4\u8fc7\u5927\u3002<\/li>\n<li><strong>\u5185\u5b58\u63a7\u5236<\/strong>\uff1a\u9884\u4f30\u5206\u7ec4\u6570\u636e\u91cf\uff0c\u5fc5\u8981\u65f6\u52a0 <code>$limit<\/code> \u6216 <code>allowDiskUse<\/code>\u3002<\/li>\n<li><strong>\u907f\u514d <code>$unwind<\/code> \u81a8\u80c0<\/strong>\uff1a\u5982\u679c\u6570\u7ec4\u5143\u7d20\u591a\uff0c\u8003\u8651\u7528 <code>$reduce<\/code> \u9884\u8ba1\u7b97\u6216\u6539\u7528\u5176\u4ed6\u6570\u636e\u6a21\u578b\u3002<\/li>\n<li><strong>\u5b9a\u671f\u5206\u6790<\/strong>\uff1a\u7528 <code>explain()<\/code> \u68c0\u67e5\u6162\u67e5\u8be2\uff0c\u5173\u6ce8 <code>totalDocsExamined<\/code> \u4e0e\u8fd4\u56de\u6587\u6863\u6570\u7684\u6bd4\u4f8b\u3002<\/li>\n<\/ol>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li>MongoDB \u5b98\u65b9\u6587\u6863\uff1aAggregation Pipeline Optimization<\/li>\n<li><code>$facet<\/code> \u5b9e\u73b0\u591a\u7ef4\u5ea6\u805a\u5408\uff08\u5982\u540c\u65f6\u8ba1\u7b97\u603b\u6570\u548c\u5206\u7ec4\u7edf\u8ba1\uff09<\/li>\n<li>\u4f7f\u7528 <code>$merge<\/code> \u5c06\u805a\u5408\u7ed3\u679c\u5199\u5165\u65b0\u96c6\u5408\uff0c\u5b9e\u73b0\u7269\u5316\u89c6\u56fe<\/li>\n<li>\u5206\u7247\u96c6\u7fa4\u4e0b\u7684\u805a\u5408\u6027\u80fd\u8c03\u4f18<\/li>\n<\/ul>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u5b9e\u6218\u6307\u5357\u80fd\u5e2e\u4f60\u5199\u51fa\u66f4\u9ad8\u6548\u7684\u805a\u5408\u67e5\u8be2\u3002\u5982\u679c\u4f60\u6709\u66f4\u597d\u7684\u4f18\u5316\u6280\u5de7\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u5206\u4eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u7535\u5546\u8ba2\u5355\u5206\u6790\u573a\u666f\uff0c\u624b\u628a\u624b\u6559\u4f60\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u8fdb\u884c\u9ad8\u6548\u6570\u636e\u5206\u6790\u3002\u6db5\u76d6 $match\u3001$group\u3001$lookup \u7b49\u6838\u5fc3\u9636\u6bb5\uff0c\u5e76\u5206\u4eab\u7d22\u5f15\u4f18\u5316\u4e0e\u5185\u5b58\u63a7\u5236\u7684\u5b9e\u6218\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,225],"tags":[64,227,112,115,226],"class_list":["post-251","post","type-post","status-publish","format-standard","hentry","category-4","category-225","tag-mongodb","tag-nosql","tag-112","tag-115","tag-226"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=251"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}