{"id":247414,"date":"2024-01-02T21:56:25","date_gmt":"2024-01-02T13:56:25","guid":{"rendered":"https:\/\/www.idc.net\/help\/247414\/"},"modified":"2024-01-02T21:56:25","modified_gmt":"2024-01-02T13:56:25","slug":"mongodb-%e6%9c%af%e8%af%ad-%e9%98%b6%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/247414\/","title":{"rendered":"MongoDB \u672f\u8bed &#8211; \u9636\u6bb5"},"content":{"rendered":"<p><title>MongoDB \u672f\u8bed - \u9636\u6bb5<\/title><\/p>\n<h1 id=\"mongodb-%e6%9c%af%e8%af%ad-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> MongoDB \u672f\u8bed - \u9636\u6bb5 <\/h1>\n<h2 id=\"%e4%bb%80%e4%b9%88%e6%98%af-mongodb-%e9%98%b6%e6%ae%b5%ef%bc%9f-nLfSsdNtlU\"> \u4ec0\u4e48\u662f MongoDB \u9636\u6bb5\uff1f<\/h2>\n<p> \u5728 MongoDB \u4e2d\uff0c\u9636\u6bb5\uff08Stage\uff09\u662f\u6307\u5728\u805a\u5408\u7ba1\u9053\uff08Aggregation Pipeline\uff09\u4e2d\u7684\u4e00\u4e2a\u64cd\u4f5c\u6b65\u9aa4\u3002\u805a\u5408\u7ba1\u9053\u662f\u4e00\u79cd\u6570\u636e\u5904\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5bf9 MongoDB \u4e2d\u7684\u6587\u6863\u8fdb\u884c\u591a\u4e2a\u64cd\u4f5c\u7684\u8fde\u7eed\u5904\u7406\u3002\u6bcf\u4e2a\u9636\u6bb5\u90fd\u4f1a\u5bf9\u8f93\u5165\u6587\u6863\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u9636\u6bb5\uff0c\u6700\u7ec8\u751f\u6210\u6240\u9700\u7684\u8f93\u51fa\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e7%94%a8%e7%9a%84-mongodb-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> \u5e38\u7528\u7684 MongoDB \u9636\u6bb5 <\/h2>\n<p> MongoDB \u63d0\u4f9b\u4e86\u591a\u4e2a\u5e38\u7528\u7684\u9636\u6bb5\uff0c\u7528\u4e8e\u5728\u805a\u5408\u7ba1\u9053\u4e2d\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684 MongoDB \u9636\u6bb5\uff1a<\/p>\n<h3 id=\"match-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> $match \u9636\u6bb5 <\/h3>\n<p> $match \u9636\u6bb5\u7528\u4e8e\u7b5b\u9009\u6ee1\u8db3\u6307\u5b9a\u6761\u4ef6\u7684\u6587\u6863\u3002\u5b83\u7c7b\u4f3c\u4e8e SQL \u4e2d\u7684 WHERE \u5b50\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u67e5\u8be2\u6761\u4ef6\u6765\u8fc7\u6ee4\u6587\u6863\u3002<\/p>\n<pre><code>\ndb.collection.aggregate([\n    { $match: { field: value } }\n])\n<\/code><\/pre>\n<h3 id=\"group-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> $group \u9636\u6bb5 <\/h3>\n<p> $group \u9636\u6bb5\u7528\u4e8e\u5bf9\u6587\u6863\u8fdb\u884c\u5206\u7ec4\u64cd\u4f5c\u3002\u5b83\u7c7b\u4f3c\u4e8e SQL \u4e2d\u7684 GROUP BY \u5b50\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u6587\u6863\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u8fdb\u884c\u805a\u5408\u8ba1\u7b97\u3002<\/p>\n<pre><code>\ndb.collection.aggregate([\n    { $group: { _id: \"$field\", count: { $sum: 1 } } }\n])\n<\/code><\/pre>\n<h3 id=\"project-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> $project \u9636\u6bb5 <\/h3>\n<p> $project \u9636\u6bb5\u7528\u4e8e\u5bf9\u6587\u6863\u8fdb\u884c\u6295\u5f71\u64cd\u4f5c\uff0c\u5373\u9009\u62e9\u9700\u8981\u7684\u5b57\u6bb5\u5e76\u6307\u5b9a\u8f93\u51fa\u7684\u683c\u5f0f\u3002\u5b83\u7c7b\u4f3c\u4e8e SQL \u4e2d\u7684 SELECT \u5b50\u53e5\uff0c\u53ef\u4ee5\u5bf9\u6587\u6863\u8fdb\u884c\u5b57\u6bb5\u9009\u62e9\u548c\u91cd\u547d\u540d\u3002<\/p>\n<pre><code>\ndb.collection.aggregate([\n    { $project: { field1: 1, field2: 1 } }\n])\n<\/code><\/pre>\n<h3 id=\"sort-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> $sort \u9636\u6bb5 <\/h3>\n<p> $sort \u9636\u6bb5\u7528\u4e8e\u5bf9\u6587\u6863\u8fdb\u884c\u6392\u5e8f\u64cd\u4f5c\u3002\u5b83\u7c7b\u4f3c\u4e8e SQL \u4e2d\u7684 ORDER BY \u5b50\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u6587\u6863\u8fdb\u884c\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<pre><code>\ndb.collection.aggregate([\n    { $sort: { field: 1 } }\n])\n<\/code><\/pre>\n<h3 id=\"limit-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> $limit \u9636\u6bb5 <\/h3>\n<p> $limit \u9636\u6bb5\u7528\u4e8e\u9650\u5236\u8f93\u51fa\u6587\u6863\u7684\u6570\u91cf\u3002\u5b83\u7c7b\u4f3c\u4e8e SQL \u4e2d\u7684 LIMIT \u5b50\u53e5\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u6587\u6863\u7684\u6700\u5927\u6570\u91cf\u3002<\/p>\n<pre><code>\ndb.collection.aggregate([\n    { $limit: 10 }\n])\n<\/code><\/pre>\n<h3 id=\"skip-%e9%98%b6%e6%ae%b5-nLfSsdNtlU\"> $skip \u9636\u6bb5 <\/h3>\n<p> $skip \u9636\u6bb5\u7528\u4e8e\u8df3\u8fc7\u6307\u5b9a\u6570\u91cf\u7684\u6587\u6863\u3002\u5b83\u7c7b\u4f3c\u4e8e SQL \u4e2d\u7684 OFFSET \u5b50\u53e5\uff0c\u53ef\u4ee5\u8df3\u8fc7\u6307\u5b9a\u6570\u91cf\u7684\u6587\u6863\u5e76\u8fd4\u56de\u5269\u4f59\u7684\u6587\u6863\u3002<\/p>\n<pre><code>\ndb.collection.aggregate([\n    { $skip: 10 }\n])\n<\/code><\/pre>\n<h2 id=\"%e6%80%bb%e7%bb%93-nLfSsdNtlU\"> \u603b\u7ed3 <\/h2>\n<p> MongoDB \u7684\u805a\u5408\u7ba1\u9053\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u9636\u6bb5\u7684\u8fde\u7eed\u5904\u7406\u6765\u5bf9\u6587\u6863\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u8f6c\u6362\u548c\u8ba1\u7b97\u3002\u5728\u805a\u5408\u7ba1\u9053\u4e2d\uff0c\u9636\u6bb5\u662f\u6307\u6bcf\u4e2a\u64cd\u4f5c\u6b65\u9aa4\uff0c\u5e38\u7528\u7684\u9636\u6bb5\u5305\u62ec $match\u3001$group\u3001$project\u3001$sort\u3001$limit \u548c $skip\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u9636\u6bb5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<h2 id=\"%e9%a6%99%e6%b8%af%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%a6%96%e9%80%89%e5%90%8e%e6%b5%aa%e4%ba%91-nLfSsdNtlU\"> \u9999\u6e2f\u670d\u52a1\u5668\u9996\u9009\u540e\u6d6a\u4e91 <\/h2>\n<p> \u540e\u6d6a\u4e91\u662f\u4e00\u5bb6\u4e13\u4e1a\u7684\u4e91\u8ba1\u7b97\u516c\u53f8\uff0c\u63d0\u4f9b\u9ad8\u6027\u80fd\u7684\u9999\u6e2f\u670d\u52a1\u5668\u3002\u4f5c\u4e3a\u4e00\u5bb6\u9886\u5148\u7684\u4e91\u670d\u52a1\u63d0\u4f9b\u5546\uff0c\u540e\u6d6a\u4e91\u7684\u9999\u6e2f\u670d\u52a1\u5668\u5177\u6709\u7a33\u5b9a\u53ef\u9760\u3001\u9ad8\u6027\u80fd\u548c\u5b89\u5168\u53ef\u63a7\u7684\u7279\u70b9\u3002\u65e0\u8bba\u662f\u4e2a\u4eba\u7528\u6237\u8fd8\u662f\u4f01\u4e1a\u7528\u6237\uff0c\u9009\u62e9\u540e\u6d6a\u4e91\u7684\u9999\u6e2f\u670d\u52a1\u5668\u90fd\u80fd\u83b7\u5f97\u5353\u8d8a\u7684\u6027\u80fd\u548c\u53ef\u9760\u7684\u670d\u52a1\u3002<\/p>\n<p> \u6b32\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u540e\u6d6a\u4e91\u7684\u9999\u6e2f\u670d\u52a1\u5668\uff0c\u8bf7\u8bbf\u95ee<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u672f\u8bed - \u9636\u6bb5 MongoDB \u672f\u8bed - \u9636\u6bb5 \u4ec0\u4e48\u662f MongoDB \u9636\u6bb5\uff1f \u5728 Mongo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-247414","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/247414","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=247414"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/247414\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=247414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=247414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=247414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}