{"id":7672,"date":"2024-11-14T11:33:43","date_gmt":"2024-11-14T03:33:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7672\/"},"modified":"2024-11-14T11:33:43","modified_gmt":"2024-11-14T03:33:43","slug":"mongodb%e6%96%87%e6%a1%a3%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9f%a5%e8%af%a2meta%e5%ad%97%e6%ae%b5%e4%b8%8b%e5%ad%90%e5%ad%97%e6%ae%b5timestampoccur%e7%ac%a6%e5%90%88%e6%8c%87%e5%ae%9a%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7672\/","title":{"rendered":"MongoDB\u6587\u6863\u4e2d\u5982\u4f55\u67e5\u8be2meta\u5b57\u6bb5\u4e0b\u5b50\u5b57\u6bb5timestampOccur\u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173113384273590.jpg\" class=\"aligncenter\" title=\"MongoDB\u6587\u6863\u4e2d\u5982\u4f55\u67e5\u8be2meta\u5b57\u6bb5\u4e0b\u5b50\u5b57\u6bb5timestampOccur\u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"MongoDB\u6587\u6863\u4e2d\u5982\u4f55\u67e5\u8be2meta\u5b57\u6bb5\u4e0b\u5b50\u5b57\u6bb5timestampOccur\u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u6587\u6863\u7684\u590d\u6742\u67e5\u8be2\uff1a\u67e5\u8be2meta\u5b57\u6bb5timestampoccur<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u5982\u4e0b\u7ed3\u6784\u6570\u636e\u7684mongodb\u6587\u6863\uff1a<\/p>\n<pre>{\n  \/\/ ... \u7701\u7565\u5176\u4ed6\u5b57\u6bb5\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u4f55\u5728mongodb\u4e2d\u67e5\u8be2\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u67e5\u8be2meta\u5b57\u6bb5\u4e0b\u5404\u5b57\u6bb5\u7684timestampoccur<\/li>\n<li>\u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684\u8bb0\u5f55<\/li>\n<\/ul>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0bmongodb\u805a\u5408\u7ba1\u9053\u53ef\u6ee1\u8db3\u60a8\u7684\u8981\u6c42\uff1a<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4ee3\u7801\u6ce8\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>$match\uff1a\u5339\u914d\u7b26\u5408\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\u3002<\/li>\n<li>$project\uff1a\u5c55\u5f00meta\u5bf9\u8c61\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/li>\n<li>$unwind\uff1a\u5c06meta\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/li>\n<li>$group\uff1a\u91cd\u65b0\u6784\u5efameta\u5bf9\u8c61\u3002<\/li>\n<li>$project\uff1a\u5c06meta\u5bf9\u8c61\u8f6c\u6362\u56de\u539f\u59cb\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fMongoDB\u6587\u6863\u4e2d\u5982\u4f55\u67e5\u8be2meta\u5b57\u6bb5\u4e0b\u5b50\u5b57\u6bb5timestampOccur\u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u6863\u7684\u590d\u6742\u67e5\u8be2\uff1a\u67e5\u8be2meta\u5b57\u6bb5timestampoccur \u95ee\u9898\uff1a \u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u5982\u4e0b\u7ed3\u6784\u6570\u636e\u7684mongodb\u6587\u6863\uff1a { \/\/ &#8230; \u7701\u7565\u5176\u4ed6\u5b57\u6bb5 &#8220;meta&#8221;: { &#8220;aaa&#8221;: { \/\/ \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684aaa\uff0c\u6bcf\u6761\u6570\u636e\u90fd\u4e0d\u4e00\u6837\uff0c\u968f\u673a\u7684\u540d\u79f0 &#8220;timestampoccur&#8221;: [ { &#8220;$date&#8221;: &#8220;2023-06-19t10:18:02.000z&#8221; \/\/ \u8fd9\u4e2a\u662f date \u7c7b\u578b } ] }, &#8220;bbb&#8221;: { &#8220;timestampoccur&#8221;: [ { &#8220;$date&#8221;: &#8220;2023-06-19t02:59:05.000z&#8221; } ] } } } \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u5728mongodb\u4e2d\u67e5\u8be2\u4ee5\u4e0b\u5185\u5bb9\uff1a \u67e5\u8be2meta\u5b57\u6bb5\u4e0b\u5404\u5b57\u6bb5\u7684timestampoccur \u7b26\u5408\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684\u8bb0\u5f55 \u89e3\u7b54\uff1a \u4ee5\u4e0bmongodb\u805a\u5408\u7ba1\u9053\u53ef\u6ee1\u8db3\u60a8\u7684\u8981\u6c42\uff1a db.collection.aggregate([ { $match: { &#8220;meta.timestampOccur&#8221;: { $elemMatch: { $gte: ISODate(&#8220;2023-06-18T00:00:00.000Z&#8221;), $lt: ISODate(&#8220;2023-06-20T00:00:00.000Z&#8221;) [&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-7672","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7672","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=7672"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7672\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}