{"id":110618,"date":"2020-06-01T18:53:06","date_gmt":"2020-06-01T10:53:06","guid":{"rendered":"http:\/\/4563.org\/?p=110618"},"modified":"2020-06-01T18:53:06","modified_gmt":"2020-06-01T10:53:06","slug":"mongodb-aggregation-pipeline-%e5%b9%b6%e6%b2%a1%e6%9c%89%e4%bd%bf%e7%94%a8%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=110618","title":{"rendered":"MongoDB Aggregation Pipeline \u5e76\u6ca1\u6709\u4f7f\u7528\u7d22\u5f15"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  MongoDB Aggregation Pipeline \u5e76\u6ca1\u6709\u4f7f\u7528\u7d22\u5f15               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u62e5\u6709\u4e94\u767e\u4e07\u4e2a\u6587\u6863\u7684\u96c6\u5408\uff0c\u6587\u6863\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code>{     \"_id\" : ObjectId(\"5ed23bd292845eaf1e488965\"),     \"categoryId\" : 18790,     \"targetType\" : 43,     \"targetId\" : 433,     \"priority\" : 0 } <\/code><\/pre>\n<p>\u96c6\u5408\u62e5\u6709\u7d22\u5f15<code>categoryId_1_priority_-1<\/code>\u3002<\/p>\n<p>\u6211\u671f\u671b\u5982\u4e0b\u7684 Aggregation Pipeline \u4f7f\u7528\u5230\u7d22\u5f15\uff0c\u4f46\u662f\u4e8b\u5b9e\u4e0a\u5e76\u6ca1\u6709\u3002<\/p>\n<pre><code>db.collection.aggregate([{$facet: {\"category_1_order_by_priority_limit_2\": [{$match: {categoryId: 1}}, {$sort: {priority: -1}}, {$limit: 2}],\"category_2_order_by_priority_limit_3\": [{$match: {categoryId: 2}}, {$sort: {priority: -1}}, {$limit: 3}]}}]) <\/code><\/pre>\n<p>explain \u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>{     \"stages\" : [         {             \"$cursor\" : {                 \"query\" : {                  },                 \"queryPlanner\" : {                     \"plannerVersion\" : 1,                     \"namespace\" : \"test.collection\",                     \"indexFilterSet\" : false,                     \"parsedQuery\" : {                      },                     \"queryHash\" : \"8B3D4AB8\",                     \"planCacheKey\" : \"8B3D4AB8\",                     \"winningPlan\" : {                         \"stage\" : \"COLLSCAN\",                         \"direction\" : \"forward\"                     },                     \"rejectedPlans\" : [ ]                 }             }         },         {             \"$facet\" : {                 ...             }         }     ],     \"serverInfo\" : {         ...     },     \"ok\" : 1 } <\/code><\/pre>\n<p>\u6211\u5e94\u8be5\u600e\u4e48\u505a\u624d\u80fd\u4f7f\u7528\u5230\u7d22\u5f15\u5462\uff1f<\/p>\n<p>indexing &#8211; MongoDB Aggregation Pipeline $match $sort doesn&#8217;t use index &#8211; Stack Overflow<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1843859\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             From [[SERVER-30474] leading $facet with each facet beginning with $match should add $match before $facet &#8211; MongoDB]( https:\/\/jira.mongodb.org\/browse\/SERVER-30474), *$facet is not smart enough to use an index. if the only stage in a pipeline is $facet, the planning system will simply default to a COLLSCAN.*                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1843860\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \uff1f\u6211\u6709\u4e00\u70b9\u7591\u95ee\uff0c\u8fd9\u4e2a\u95ee\u9898\u5df2\u7ecf\u6709\u7b54\u6848\u4e86\uff08\u89c1\u9644\u8a00\uff09\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u6709\u4eba\u6536\u85cf\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB Aggregati&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/110618"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110618"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/110618\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110618"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}