{"id":7585,"date":"2024-11-14T10:32:28","date_gmt":"2024-11-14T02:32:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7585\/"},"modified":"2024-11-14T10:32:28","modified_gmt":"2024-11-14T02:32:28","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-mongodb-%e4%b8%ad%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-%e5%ad%97%e6%ae%b5%e4%b8%ad-timestampoccur-%e5%80%bc%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7585\/","title":{"rendered":"\u5982\u4f55\u5728 MongoDB \u4e2d\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta \u5b57\u6bb5\u4e2d timestampOccur \u503c\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173104192386499.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 MongoDB \u4e2d\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta \u5b57\u6bb5\u4e2d timestampOccur \u503c\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 MongoDB \u4e2d\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta \u5b57\u6bb5\u4e2d timestampOccur \u503c\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u67e5\u8be2  \u4e2d\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u7f16\u5199 mongodb \u67e5\u8be2\uff0c\u4ee5\u4ece\u7ed9\u5b9a\u7684\u6587\u6863\u7ed3\u6784\u4e2d\u83b7\u53d6\u5728\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5\u4e2d\u7684 timestampoccur \u503c\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u4e0b\u9762\u63d0\u4f9b\u7684\u4ee3\u7801\u63d0\u4f9b\u4e86\u4e00\u4e2a\u805a\u5408\u7ba1\u9053\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u67e5\u8be2\uff1a<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>$match \u7528\u4e8e\u5339\u914d\u7b26\u5408\u65e5\u671f\u8303\u56f4\u5185\u7684\u8bb0\u5f55\u3002<\/li>\n<li>$project \u7528\u4e8e\u5c55\u5f00 meta \u5bf9\u8c61\u548c\u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/li>\n<li>$unwind \u7528\u4e8e\u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/li>\n<li>$match \u518d\u6b21\u7528\u4e8e\u7b5b\u9009\u7b26\u5408\u65e5\u671f\u8303\u56f4\u7684\u8bb0\u5f55\u3002<\/li>\n<li>$group \u7528\u4e8e\u91cd\u65b0\u6784\u5efa meta \u5bf9\u8c61\u3002<\/li>\n<li>$project \u7528\u4e8e\u5c06 meta \u5bf9\u8c61\u8f6c\u6362\u56de\u539f\u59cb\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 MongoDB \u4e2d\u67e5\u8be2\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185 meta \u5b57\u6bb5\u4e2d timestampOccur \u503c\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>\u5982\u4f55\u67e5\u8be2 \u4e2d\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55 \u95ee\u9898\uff1a \u5982\u4f55\u7f16\u5199 mongodb \u67e5\u8be2\uff0c\u4ee5\u4ece\u7ed9\u5b9a\u7684\u6587\u6863\u7ed3\u6784\u4e2d\u83b7\u53d6\u5728\u6307\u5b9a\u65e5\u671f\u8303\u56f4\u5185\u7684 meta \u5b57\u6bb5\u4e2d\u7684 timestampoccur \u503c\uff1f \u89e3\u7b54\uff1a \u4e0b\u9762\u63d0\u4f9b\u7684\u4ee3\u7801\u63d0\u4f9b\u4e86\u4e00\u4e2a\u805a\u5408\u7ba1\u9053\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u8ff0\u67e5\u8be2\uff1a db.collection.aggregate([ \/\/ \u5339\u914d\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55 { $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;) } } } }, \/\/ \u5c55\u5f00 meta \u5bf9\u8c61\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c { $project: { _id: 1, campaign: 1, status: 1, workflow: 1, user: 1, basic: 1, type: 1, createdAt: 1, updatedAt: 1, [&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-7585","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7585","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=7585"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7585\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}