{"id":48958,"date":"2024-12-02T14:59:30","date_gmt":"2024-12-02T06:59:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48958\/"},"modified":"2024-12-02T14:59:30","modified_gmt":"2024-12-02T06:59:30","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-php-%e5%ae%9e%e7%8e%b0-mongodb-%e4%b8%ad%e7%9a%84-substr-%e6%93%8d%e4%bd%9c%e7%ac%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48958\/","title":{"rendered":"\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/17307707936729776944e00.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>mongo \u4e2d $substr \u64cd\u4f5c\u7b26\u7684 php \u5b9e\u73b0<\/strong><\/p>\n<p>\u4e3a\u4e86\u5c06 mongodb \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\u8f6c\u6362\u4e3a php \u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528 mongo_collelection \u7684 aggregate \u65b9\u6cd5\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u805a\u5408\u6d41\u6c34\u7ebf\uff1a<\/p>\n<pre>$pipeline = [\n    ['$project' =&gt; [\n        'date' =&gt; ['$substr' =&gt; ['$time_start', 0, 10]], \n        \/\/\u53d6\u524d\u5341\u5b57\u7b26\n        'income' =&gt; 1, \n        \/\/\u6536\u5165\u5b57\u6bb5\u7528\u4e8e\u540e\u9762\u7684\u805a\u5408\n    ]],\n    ['$group' =&gt; [\n        '_id' =&gt; ['$date'], \n        \/\/\u6309\u65e5\u671f\u5206\u7ec4\n        'sumincome' =&gt; ['$sum' =&gt; '$income'], \n        \/\/\u6c42\u6536\u5165\u603b\u548c\n    ]]\n];<\/pre>\n<p>\u7136\u540e\uff0c\u8c03\u7528 aggregate \u65b9\u6cd5\u6267\u884c\u805a\u5408\u5e76\u83b7\u53d6\u7ed3\u679c\uff1a<\/p>\n<pre>$result = $this-&gt;mongo_Collelection-&gt;aggregate($pipeline);<\/pre>\n<p>\u6700\u7ec8\u7ed3\u679c\u662f\u6309\u65e5\u671f\u5206\u7ec4\u540e\u7684\u6536\u5165\u603b\u548c\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300a\u5982\u4f55\u7528 PHP \u5b9e\u73b0 MongoDB \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\uff1f\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 mongo \u4e2d $substr \u64cd\u4f5c\u7b26\u7684 php \u5b9e\u73b0 \u4e3a\u4e86\u5c06 mongodb \u4e2d\u7684 $substr \u64cd\u4f5c\u7b26\u8f6c\u6362\u4e3a php \u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528 mongo_collelection \u7684 aggregate \u65b9\u6cd5\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a \u9996\u5148\uff0c\u5b9a\u4e49\u805a\u5408\u6d41\u6c34\u7ebf\uff1a $pipeline = [ [&#8216;$project&#8217; =&gt; [ &#8216;date&#8217; =&gt; [&#8216;$substr&#8217; =&gt; [&#8216;$time_start&#8217;, 0, 10]], \/\/\u53d6\u524d\u5341\u5b57\u7b26 &#8216;income&#8217; =&gt; 1, \/\/\u6536\u5165\u5b57\u6bb5\u7528\u4e8e\u540e\u9762\u7684\u805a\u5408 ]], [&#8216;$group&#8217; =&gt; [ &#8216;_id&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48958","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48958"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48958\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}