{"id":184932,"date":"2023-06-15T12:55:11","date_gmt":"2023-06-15T04:55:11","guid":{"rendered":"http:\/\/www.idc.net\/help\/184932\/"},"modified":"2023-06-15T12:55:11","modified_gmt":"2023-06-15T04:55:11","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%95%99%e7%a8%8b%ef%bc%9asdk%e6%95%b0%e6%8d%ae%e5%ba%93-aggregate%c2%b7%e8%bf%94%e5%9b%9e%e6%8e%92%e5%ba%8f%e5%90%8e%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/184932\/","title":{"rendered":"\u540e\u6d6a\u4e91\u5c0f\u7a0b\u5e8f\u6559\u7a0b\uff1aSDK\u6570\u636e\u5e93 Aggregate\u00b7\u8fd4\u56de\u6392\u5e8f\u540e\u7ed3\u679c"},"content":{"rendered":"<h2>Aggregate.sortByCount(object:Object):&nbsp;Aggregate<\/h2>\n<blockquote><p>\n  \u652f\u6301\u7aef\uff1a\u5c0f\u7a0b\u5e8f 2.7.4,&nbsp;\u4e91\u51fd\u6570 0.8.1,&nbsp;Web\n<\/p><\/blockquote>\n<p>\u805a\u5408\u9636\u6bb5\u3002\u6839\u636e\u4f20\u5165\u7684\u8868\u8fbe\u5f0f\uff0c\u5c06\u4f20\u5165\u7684\u96c6\u5408\u8fdb\u884c\u5206\u7ec4\uff08group\uff09\u3002\u7136\u540e\u8ba1\u7b97\u4e0d\u540c\u7ec4\u7684\u6570\u91cf\uff0c\u5e76\u4e14\u5c06\u8fd9\u4e9b\u7ec4\u6309\u7167\u5b83\u4eec\u7684\u6570\u91cf\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<h2>\u53c2\u6570<\/h2>\n<h3>object: Object<\/h3>\n<h2>\u8fd4\u56de\u503c<\/h2>\n<h3>Aggregate<\/h3>\n<h2>API \u8bf4\u660e<\/h2>\n<p>sortByCount&nbsp;\u7684\u8c03\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>sortByCount(&lt;\u8868\u8fbe\u5f0f&gt;)\n<\/code><\/pre>\n<p>\u8868\u8fbe\u5f0f\u7684\u5f62\u5f0f\u662f\uff1a$ + \u6307\u5b9a\u5b57\u6bb5\u3002\u8bf7\u6ce8\u610f\uff1a\u4e0d\u8981\u6f0f\u5199&nbsp;$&nbsp;\u7b26\u53f7\u3002<\/p>\n<h2>\u793a\u4f8b<\/h2>\n<h4>\u7edf\u8ba1\u57fa\u7840\u7c7b\u578b<\/h4>\n<p>\u5047\u8bbe\u96c6\u5408&nbsp;passages&nbsp;\u7684\u8bb0\u5f55\u5982\u4e0b\uff1a<\/p>\n<pre><code>{ \"category\": \"Web\" }\n{ \"category\": \"Web\" }\n{ \"category\": \"Life\" }\n<\/code><\/pre>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u7edf\u8ba1\u6587\u7ae0\u7684\u5206\u7c7b\u4fe1\u606f\uff0c\u5e76\u4e14\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7c7b\u7684\u6570\u91cf\u3002\u5373\u5bf9&nbsp;category&nbsp;\u5b57\u6bb5\u6267\u884c&nbsp;sortByCount&nbsp;\u805a\u5408\u64cd\u4f5c\u3002<\/p>\n<pre><code>db.collection('passages')\n  .aggregate()\n  .sortByCount('$category')\n  .end()\n<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1aWeb&nbsp;\u5206\u7c7b\u4e0b\u67092\u7bc7\u6587\u7ae0\uff0cLife&nbsp;\u5206\u7c7b\u4e0b\u67091\u7bc7\u6587\u7ae0\u3002<\/p>\n<pre><code>{ \"_id\": \"Web\", \"count\": 2 }\n{ \"_id\": \"Life\", \"count\": 1 }\n<\/code><\/pre>\n<h4>\u89e3\u6784\u6570\u7ec4\u7c7b\u578b<\/h4>\n<p>\u5047\u8bbe\u96c6\u5408&nbsp;passages&nbsp;\u7684\u8bb0\u5f55\u5982\u4e0b\uff1atags&nbsp;\u5b57\u6bb5\u5bf9\u5e94\u7684\u503c\u662f\u6570\u7ec4\u7c7b\u578b\u3002<\/p>\n<pre><code>{ \"tags\": [ \"JavaScript\", \"C#\" ] }\n{ \"tags\": [ \"Go\", \"C#\" ] }\n{ \"tags\": [ \"Go\", \"Python\", \"JavaScript\" ] }\n<\/code><\/pre>\n<p>\u5982\u4f55\u7edf\u8ba1\u6587\u7ae0\u7684\u6807\u7b7e\u4fe1\u606f\uff0c\u5e76\u4e14\u8ba1\u7b97\u6bcf\u4e2a\u6807\u7b7e\u7684\u6570\u91cf\uff1f\u56e0\u4e3a&nbsp;tags&nbsp;\u5b57\u6bb5\u5bf9\u5e94\u7684\u6570\u7ec4\uff0c\u6240\u4ee5\u9700\u8981\u501f\u52a9&nbsp;unwind&nbsp;\u64cd\u4f5c\u89e3\u6784&nbsp;tags&nbsp;\u5b57\u6bb5\uff0c\u7136\u540e\u518d\u8c03\u7528&nbsp;sortByCount\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u529f\u80fd\uff1a<\/p>\n<pre><code>db.collection('passages')\n  .aggregate()\n  .unwind(`$tags`)\n  .sortByCount(`$tags`)\n  .end()\n<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>{ \"_id\": \"Go\", \"count\": 2 }\n{ \"_id\": \"C#\", \"count\": 2 }\n{ \"_id\": \"JavaScript\", \"count\": 2 }\n{ \"_id\": \"Python\", \"count\": 1 }<\/code><\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aggregate.sortByCount(object:Object):&nbsp;Aggregate \u652f\u6301 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-184932","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=184932"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184932\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=184932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=184932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=184932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}