{"id":200590,"date":"2025-05-06T11:35:31","date_gmt":"2025-05-06T03:35:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200590\/"},"modified":"2025-05-06T11:35:31","modified_gmt":"2025-05-06T03:35:31","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mongodb-%e8%81%9a%e5%90%88%e7%ae%a1%e9%81%93%e6%9f%a5%e8%af%a2%e6%8c%87%e5%ae%9a%e6%97%a5%e6%9c%9f%e8%8c%83%e5%9b%b4%e5%86%85%e7%9a%84-meta-%e5%ad%97%e6%ae%b5-time","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200590\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f<\/h1>\n<p>\u6765\u5230\u4e3b\u673a\u5b9d\u8d1d\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6570\u636e\u5e93\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731121794672ed282c9935.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mongodb \u67e5\u8be2\u4e2d\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 timestampoccur<\/strong><\/p>\n<p>\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8be5\u95ee\u9898\u65e8\u5728\u67e5\u8be2 meta \u4e0b\u5404\u5b57\u6bb5\u7684 timestampoccur\uff0c\u5e76\u7b26\u5408\u6307\u5b9a\u7684\u65e5\u671f\u8303\u56f4\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 mongodb \u7684\u805a\u5408\u7ba1\u9053\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p><strong>\u805a\u5408\u7ba1\u9053\uff1a<\/strong><\/p>\n<pre>db.collection.aggregate([\n  {\n    $match: {\n      \"meta.timestampOccur\": {\n        $elemMatch: {\n          $gte: ISODate(\"2023-06-18T00:00:00.000Z\"),\n          $lt: ISODate(\"2023-06-20T00:00:00.000Z\")\n        }\n      }\n    }\n  },\n  {\n    $project: {\n      _id: 1,\n      campaign: 1,\n      status: 1,\n      workflow: 1,\n      user: 1,\n      basic: 1,\n      type: 1,\n      createdAt: 1,\n      updatedAt: 1,\n      tag: 1,\n      code: 1,\n      meta: {\n        $objectToArray: \"$meta\"\n      }\n    }\n  },\n  {\n    $unwind: {\n      path: \"$meta\",\n      includeArrayIndex: \"metaIndex\"\n    }\n  },\n  {\n    $match: {\n      \"meta.v.timestampOccur\": {\n        $elemMatch: {\n          $gte: ISODate(\"2023-06-18T00:00:00.000Z\"),\n          $lt: ISODate(\"2023-06-20T00:00:00.000Z\")\n        }\n      }\n    }\n  },\n  {\n    $group: {\n      _id: \"$_id\",\n      campaign: { $first: \"$campaign\" },\n      status: { $first: \"$status\" },\n      workflow: { $first: \"$workflow\" },\n      user: { $first: \"$user\" },\n      basic: { $first: \"$basic\" },\n      type: { $first: \"$type\" },\n      createdAt: { $first: \"$createdAt\" },\n      updatedAt: { $first: \"$updatedAt\" },\n      tag: { $first: \"$tag\" },\n      code: { $first: \"$code\" },\n      meta: {\n        $push: {\n          k: \"$meta.k\",\n          v: \"$meta.v\"\n        }\n      }\n    }\n  },\n  {\n    $project: {\n      _id: 1,\n      campaign: 1,\n      status: 1,\n      workflow: 1,\n      user: 1,\n      basic: 1,\n      type: 1,\n      createdAt: 1,\n      updatedAt: 1,\n      tag: 1,\n      code: 1,\n      meta: {\n        $arrayToObject: \"$meta\"\n      }\n    }\n  }\n])<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li><strong>$match\uff1a<\/strong>\u5339\u914d\u7b26\u5408\u65e5\u671f\u8303\u56f4\u5185\u7684\u8bb0\u5f55\u3002<\/li>\n<li><strong>$project\uff1a<\/strong>\u5c55\u5f00meta\u5bf9\u8c61\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/li>\n<li><strong>$unwind\uff1a<\/strong>\u5c06meta\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/li>\n<li><strong>$match\uff1a<\/strong>\u5982\u679cmeta.timestampoccur\u7b26\u5408\u65e5\u671f\u8303\u56f4\uff0c\u5219\u8f93\u51fa\u8bb0\u5f55\u3002<\/li>\n<li><strong>$group\uff1a<\/strong>\u91cd\u65b0\u6784\u5efameta\u5bf9\u8c61\u3002<\/li>\n<li><strong>$project\uff1a<\/strong>\u5c06meta\u5bf9\u8c61\u8f6c\u6362\u56de\u539f\u59cb\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6570\u636e\u5e93\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200590","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200590"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200590\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}