{"id":200867,"date":"2025-05-06T12:13:30","date_gmt":"2025-05-06T04:13:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200867\/"},"modified":"2025-05-06T12:13:30","modified_gmt":"2025-05-06T04:13:30","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mongodb-%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-meta-timestampoccur-%e5%ad%97%e6%ae%b5%e7%9a%84%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200867\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MongoDB \u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta.timestampOccur \u5b57\u6bb5\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 MongoDB \u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta.timestampOccur \u5b57\u6bb5\u7684\u8bb0\u5f55\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731906056673aca084cad2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u67e5\u8be2 mongodb \u4e2d\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta.timestampoccur<\/strong><\/p>\n<p>\u5982\u4f55\u67e5\u8be2\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\u7684\u6570\u636e\u8bb0\u5f55\u4e2d\uff0cmeta \u5b57\u6bb5\u4e0b\u5404 timestampoccur \u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\uff1f<\/p>\n<pre>{\n  ...\n  \"meta\": {\n    \"aaa\": { \/\/ \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684aaa\uff0c\u6bcf\u6761\u6570\u636e\u90fd\u4e0d\u4e00\u6837\uff0c\u968f\u673a\u7684\u540d\u79f0\n      \"timestampoccur\": [\n        {\n          \"$date\": \"2023-06-19t10:18:02.000z\"  \/\/ \u8fd9\u4e2a\u662f date \u7c7b\u578b\n        }\n      ]\n    },\n    \"bbb\": {\n      \"timestampoccur\": [\n        {\n          \"$date\": \"2023-06-19t02:59:05.000z\"\n        }\n      ]\n    }\n  }\n}<\/pre>\n<p><strong>mongodb \u67e5\u8be2<\/strong><\/p>\n<pre>db.collection.aggregate([\n  \/\/ \u5339\u914d\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\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  \/\/ \u5c55\u5f00 meta \u5bf9\u8c61\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\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  \/\/ \u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\n  {\n    $unwind: {\n      path: \"$meta\",\n      includeArrayIndex: \"metaIndex\"\n    }\n  },\n  \/\/ \u5982\u679c meta.timestampOccur \u7b26\u5408\u65e5\u671f\u8303\u56f4\uff0c\u5219\u8f93\u51fa\u8bb0\u5f55\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  \/\/ \u91cd\u65b0\u6784\u5efa meta \u5bf9\u8c61\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  \/\/ \u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u56de\u539f\u59cb\u683c\u5f0f\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>\u4ee3\u7801\u8be6\u89e3<\/strong><\/p>\n<ul>\n<li><strong>match<\/strong>: \u5339\u914d\u6ee1\u8db3\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u7684 meta.timestampoccur \u5b57\u6bb5\u3002<\/li>\n<li><strong>project<\/strong>: \u5c55\u5f00 meta \u5bf9\u8c61\uff0c\u4f7f\u5176\u6210\u4e3a\u4e00\u4e2a\u6570\u7ec4\uff0c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u3002<\/li>\n<li><strong>unwind<\/strong>: \u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6587\u6863\u6570\u7ec4\u3002<\/li>\n<li><strong>match<\/strong>: \u8fdb\u4e00\u6b65\u5339\u914d\u7b26\u5408\u65e5\u671f\u8303\u56f4\u7684 meta.timestampoccur \u5b57\u6bb5\u3002<\/li>\n<li><strong>group<\/strong>: \u91cd\u65b0\u6784\u5efa meta \u5bf9\u8c61\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u539f\u59cb\u683c\u5f0f\u3002<\/li>\n<li><strong>project<\/strong>: \u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u56de\u539f\u59cb\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 MongoDB \u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta.timestampOccur \u5b57\u6bb5\u7684\u8bb0\u5f55\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 MongoDB \u67e5\u8be2\u6307&#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-200867","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200867","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=200867"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200867\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}