{"id":7048,"date":"2016-06-30T11:43:22","date_gmt":"2016-06-30T02:43:22","guid":{"rendered":"http:\/\/www.skyarch.net\/blog\/?p=7048"},"modified":"2016-07-08T12:27:21","modified_gmt":"2016-07-08T03:27:21","slug":"mongodb%e3%81%ae%e6%a4%9c%e7%b4%a2%e3%81%8c%e9%81%85%e3%81%84%e3%81%a8%e3%81%8d%e3%81%aeindex%e4%bd%9c%e6%88%90%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/mongodb%e3%81%ae%e6%a4%9c%e7%b4%a2%e3%81%8c%e9%81%85%e3%81%84%e3%81%a8%e3%81%8d%e3%81%aeindex%e4%bd%9c%e6%88%90%e6%96%b9%e6%b3%95\/","title":{"rendered":"MongoDB\u306e\u691c\u7d22\u304c\u9045\u3044\u3068\u304d\u306eIndex\u4f5c\u6210\u65b9\u6cd5"},"content":{"rendered":"<p>\u4ee5\u4e0b\u306eQuery\u3092\u4f8b\u306b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ndb.test_collection.find({ &quot;content.a&quot; : &quot;aaaa&quot;, &quot;content.sub_content.b&quot; : &quot;bbbb&quot;, &quot;sex&quot; : &quot;male&quot; }).sort({ &quot;createdAt&quot; : -1 }).limit(30)\r\n<\/pre>\n<p>Index\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u306fRDB\u3068\u540c\u3058\u304f\u4ee5\u4e0b\u3092\u8003\u616e\u3059\u308b\u3002<\/p>\n<ul>\n<li>\u6700\u521d\u306b\u691c\u7d22\u6761\u4ef6\u3001\u6b21\u306b\u4e26\u3079\u66ff\u3048\u9806\u5e8f(\u4e0a\u8a18\u306e\u5834\u5408\u3001\u691c\u7d22\u6761\u4ef6\u306f\"content.a\", \"content.sub_content.b\", \"sex\"\u3001\u4e26\u3079\u66ff\u3048\u9806\u5e8f\u306f\"createdAt\")<\/li>\n<li>find\u95a2\u6570\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u8003\u616e\u3057\u3001\u691c\u7d22\u6761\u4ef6\u306f\u5927\u304d\u304f\u691c\u7d22\u5bfe\u8c61\u3092\u7d5e\u308a\u8fbc\u3081\u308b\u3082\u306e\u304b\u3089\u6307\u5b9a(\u4e0a\u8a18\u306e\u5834\u5408\u3001\"sex\"(\u6027\u5225)\u306fnull\u3092\u542b\u3081\u3066\u3082\u5024\u304c3\u7a2e\u985e\u3057\u304b\u306a\u3044\u306e\u3067\u6307\u5b9a\u3092\u5f8c\u56de\u3057\u306b\u3059\u308b)<\/li>\n<li>\u4e26\u3079\u66ff\u3048\u9806\u5e8f\u306f\u30af\u30a8\u30ea\u30fc\u304c\u6307\u5b9a\u3057\u3066\u3044\u308b\u9806(\u4e0a\u8a18\u306e\u5834\u5408\u3001\u4e26\u3079\u66ff\u3048\u306f\"createdAt\"\u306e\u964d\u9806\u306e\u307f\u3067\u3042\u308b\u304c\u3001\u8907\u6570\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u305d\u306e\u9806\u756a\u306b\u6307\u5b9a)<\/li>\n<\/ul>\n<p>\u5b9f\u969b\u306e\u624b\u9806\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/p>\n<pre>\n# mongoDB\u306b\u30ed\u30b0\u30a4\u30f3\nmongo\n&gt; # \u691c\u7d22\u5bfe\u8c61\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u79fb\u52d5\n&gt; use test_database\n&gt; # Query\u306e\u4f53\u611f\u5b9f\u884c\u901f\u5ea6\u3092\u78ba\u8a8d\n&gt; db.test_collection.find({ \"content.a\" : \"aaaa\", \"content.sub_content.b\" : \"bbbb\", \"sex\" : \"male\" }).sort({ \"createdAt\" : -1 }).limit(30)\n&gt; # Query\u3092\u5b9f\u884c\u8a08\u753b\u3092\u78ba\u8a8d\n&gt; db.test_collection.find({ \"content.a\" : \"aaaa\", \"content.sub_content.b\" : \"bbbb\", \"sex\" : \"male\" }).sort({ \"createdAt\" : -1 }).limit(30).explain()\n&gt; # \u73fe\u5728\u306eindex\u4e00\u89a7\u3092\u78ba\u8a8d\n&gt; db.test_collection.getIndexes()\n&gt; # index\u3092\u4f5c\u6210\n&gt; db.test_collection.createIndex({ \"content.a\" : 1, \"ohai_attributes.customer_info.matter_id: 1\", \"data_type\" : 1, \"createdAt\" : -1 })\n&gt; # index\u304c\u4f5c\u6210\u3067\u304d\u305f\u3053\u3068\u3092\u78ba\u8a8d\n&gt; db.test_collection.getIndexes()\n&gt; # Query\u306e\u4f53\u611f\u5b9f\u884c\u901f\u5ea6\u3092\u78ba\u8a8d\n&gt; db.test_collection.find({ \"content.a\" : \"aaaa\", \"content.sub_content.b\" : \"bbbb\", \"sex\" : \"male\" }).sort({ \"createdAt\" : -1 }).limit(30)\n&gt; # Query\u3092\u5b9f\u884c\u8a08\u753b\u3092\u78ba\u8a8d\n&gt; db.test_collection.find({ \"content.a\" : \"aaaa\", \"content.sub_content.b\" : \"bbbb\", \"sex\" : \"male\" }).sort({ \"createdAt\" : -1 }).limit(30).explain()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u306eQuery\u3092\u4f8b\u306b\u3059\u308b\u3002 db.test_collection.find({ &quot;content.a&quot; : &quot;aaaa&quot;, &quot;content.sub_content.b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":7050,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"7048","footnotes":""},"categories":[23],"tags":[],"class_list":{"0":"post-7048","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-23","8":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/7048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=7048"}],"version-history":[{"count":6,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/7048\/revisions"}],"predecessor-version":[{"id":7321,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/7048\/revisions\/7321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/7050"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=7048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=7048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=7048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}