{"id":26705,"date":"2024-11-24T10:21:10","date_gmt":"2024-11-24T02:21:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26705\/"},"modified":"2024-11-24T10:21:10","modified_gmt":"2024-11-24T02:21:10","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-ti-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26705\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173104922082186.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 MongoDB \u805a\u5408\u7ba1\u9053\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5 timestampOccur\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u8be2\u4e2d\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 timestampoccur \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 \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 \u805a\u5408\u7ba1\u9053\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;) } } } }, { $project: { _id: 1, campaign: 1, status: 1, workflow: 1, user: 1, basic: 1, type: 1, createdAt: 1, updatedAt: 1, tag: 1, code: 1, meta: { $objectToArray: &#8220;$meta&#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-26705","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26705","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=26705"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26705\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}