{"id":184938,"date":"2023-08-31T18:43:01","date_gmt":"2023-08-31T10:43:01","guid":{"rendered":"http:\/\/www.idc.net\/help\/184938\/"},"modified":"2023-08-31T18:43:01","modified_gmt":"2023-08-31T10:43:01","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%87%e6%bb%a4%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/184938\/","title":{"rendered":"\u540e\u6d6a\u4e91\u5c0f\u7a0b\u5e8f\u6559\u7a0b\uff1aSDK\u6570\u636e\u5e93 Aggregate\u00b7\u8fc7\u6ee4\u6587\u6863"},"content":{"rendered":"<h2>Aggregate.match(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\u6761\u4ef6\u8fc7\u6ee4\u6587\u6863\uff0c\u5e76\u4e14\u628a\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u6d41\u6c34\u7ebf\u9636\u6bb5\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>match&nbsp;\u7684\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>match(&lt;\u67e5\u8be2\u6761\u4ef6&gt;)\n<\/code><\/pre>\n<p>\u67e5\u8be2\u6761\u4ef6\u4e0e\u666e\u901a\u67e5\u8be2\u4e00\u81f4\uff0c\u53ef\u4ee5\u7528\u666e\u901a\u67e5\u8be2\u64cd\u4f5c\u7b26\uff0c\u6ce8\u610f&nbsp;match&nbsp;\u9636\u6bb5\u548c\u5176\u4ed6\u805a\u5408\u9636\u6bb5\u4e0d\u540c\uff0c\u4e0d\u53ef\u4f7f\u7528\u805a\u5408\u64cd\u4f5c\u7b26\uff0c\u53ea\u80fd\u4f7f\u7528\u67e5\u8be2\u64cd\u4f5c\u7b26\u3002<\/p>\n<pre><code>\/\/ \u76f4\u63a5\u4f7f\u7528\u5b57\u7b26\u4e32\nmatch({\n  name: 'Tony Stark'\n})\n\n\/\/ \u4f7f\u7528\u64cd\u4f5c\u7b26\nconst _ = db.command\nmatch({\n  age: _.gt(18)\n})\n<\/code><\/pre>\n<blockquote><p>\n  match&nbsp;\u5185\u4f7f\u7528\u67e5\u8be2\u64cd\u4f5c\u7b26\u4ece\u5c0f\u7a0b\u5e8f\u57fa\u7840\u5e93&nbsp;2.8.3\u3001\u4e91\u51fd\u6570 SDK 1.3.0 \u5f00\u59cb\u652f\u6301\u3002\n<\/p><\/blockquote>\n<h2>\u793a\u4f8b<\/h2>\n<p>\u5047\u8bbe\u96c6\u5408&nbsp;articles&nbsp;\u6709\u5982\u4e0b\u8bb0\u5f55\uff1a<\/p>\n<pre><code>{ \"_id\" : \"1\", \"author\" : \"stark\",  \"score\" : 80 }\n{ \"_id\" : \"2\", \"author\" : \"stark\",  \"score\" : 85 }\n{ \"_id\" : \"3\", \"author\" : \"bob\",    \"score\" : 60 }\n{ \"_id\" : \"4\", \"author\" : \"li\",     \"score\" : 55 }\n{ \"_id\" : \"5\", \"author\" : \"jimmy\",  \"score\" : 60 }\n{ \"_id\" : \"6\", \"author\" : \"li\",     \"score\" : 94 }\n{ \"_id\" : \"7\", \"author\" : \"justan\", \"score\" : 95 }\n<\/code><\/pre>\n<h4>\u5339\u914d<\/h4>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u76f4\u63a5\u5339\u914d\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>db.collection('articles')\n  .aggregate()\n  .match({\n    author: 'stark'\n  })\n  .end()\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u4ee3\u7801\u5c1d\u8bd5\u627e\u5230\u6240\u6709&nbsp;author&nbsp;\u5b57\u6bb5\u662f&nbsp;stark&nbsp;\u7684\u6587\u7ae0\uff0c\u90a3\u4e48\u5339\u914d\u5982\u4e0b\uff1a<\/p>\n<pre><code>{ \"_id\" : \"1\", \"author\" : \"stark\", \"score\" : 80 }\n{ \"_id\" : \"2\", \"author\" : \"stark\", \"score\" : 85 }\n<\/code><\/pre>\n<h4>\u8ba1\u6570<\/h4>\n<p>match&nbsp;\u8fc7\u6ee4\u51fa\u6587\u6863\u540e\uff0c\u8fd8\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6d41\u6c34\u7ebf\u9636\u6bb5\u914d\u5408\u4f7f\u7528\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u4f7f\u7528&nbsp;group&nbsp;\u8fdb\u884c\u642d\u914d\uff0c\u8ba1\u7b97&nbsp;score&nbsp;\u5b57\u6bb5\u5927\u4e8e&nbsp;80&nbsp;\u7684\u6587\u6863\u6570\u91cf\uff1a<\/p>\n<pre><code>const _ = db.command\nconst $ = _.aggregate\ndb.collection('articles')\n  .aggregate()\n  .match({\n    score: _.gt(80)\n  })\n  .group({\n      _id: null,\n      count: $.sum(1)\n  })\n  .end()\n<\/code><\/pre>\n<p>\u8fd4\u56de\u503c\u5982\u4e0b\uff1a<\/p>\n<pre><code>{ \"_id\" : null, \"count\" : 3 }<\/code><\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aggregate.match(object: Object):&nbsp;Aggregate \u652f\u6301\u7aef\uff1a\u5c0f\u7a0b\u5e8f [&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-184938","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184938","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=184938"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184938\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=184938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=184938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=184938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}