{"id":2457,"date":"2013-01-24T11:08:19","date_gmt":"2013-01-24T03:08:19","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2457"},"modified":"2013-01-24T11:09:31","modified_gmt":"2013-01-24T03:09:31","slug":"%e4%b8%80%e4%b8%aamongodb%e6%9f%a5%e8%af%a2%e8%bf%b7%e9%a2%98","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2457.html","title":{"rendered":"\u4e00\u4e2aMongoDB\u67e5\u8be2\u8ff7\u9898"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u672c\u6587\u6765\u81ea MongoDB \u6838\u5fc3\u5f00\u53d1\u4eba\u5458@kchodorow \u7684\u535a\u6587\uff0c\u662f\u5176\u5173\u4e8e MongoDB \u67e5\u8be2\u8ff7\u9898\u7684\u7b2c\u4e00\u7bc7\uff0c\u901a\u8fc7\u51e0\u4e2a\u4f8b\u5b50\u4ecb\u7ecd\u4e86\u5728Array \u4e2d\u8fdb\u884c\u8303\u56f4\u67e5\u8be2\u7684\u4e00\u4e9b\u67e5\u8be2\u89c4\u5219\u548c\u7528\u6cd5\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"> \u5047\u5982\u4e00\u4e2a Collection \u4e2d\u6709\u4e0b\u9762\u4e00\u4e9b\u6570\u636e\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{&quot;x&quot;: -5}\r\n{&quot;x&quot;: 0}\r\n{&quot;x&quot;: 5}\r\n{&quot;x&quot;: 10}\r\n{&quot;x&quot;: &#x5B;0, 5]}\r\n{&quot;x&quot;: &#x5B;-5, 10]}\r\n{&quot;x&quot;: &#x5B;-5, 5, 10]}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5982\u679c\u6211\u4eec\u5728\u8fd9\u4e2a Collection \u4e0a\u6267\u884c\u4e0b\u9762\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u4f1a\u8fd4\u56de\u54ea\u4e9b\u6570\u636e\u5462\uff1f<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndb.foo.find({&quot;x&quot; : {&quot;$gt&quot; : -1, &quot;$lt&quot; : 6}})\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7ed3\u679c\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{&quot;x&quot; : 0}\r\n{&quot;x&quot; : 5}\r\n{&quot;x&quot; : &#x5B;0, 5]}\r\n{&quot;x&quot; : &#x5B;-5, 10]} \/\/\u8fd9\u8d27\u4e5f\u88ab\u67e5\u51fa\u6765\u4e86\uff01\uff01\r\n{&quot;x&quot; : &#x5B;-5, 5, 10]}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0a\u9762\u7ea2\u8272\u6ce8\u91ca\u4e00\u6761\uff0c\u5176\u4e24\u4e2a\u5143\u7d20\u90fd\u6ee1\u8db3\u5927\u4e8e-1\u5c0f\u4e8e6\u7684\u6761\u4ef6\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u662f\u88ab\u67e5\u51fa\u6765\u4e86\u5462\uff1f\u8ba9\u6211\u4eec\u6765\u770b\u770b MongoDB \u5728 Array \u4e0a\u8fdb\u884c\u8303\u56f4\u67e5\u8be2\u7684\u539f\u7406\u5427\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"> MongoDB \u5728\u8fdb\u884c\u8303\u56f4\u67e5\u8be2\u65f6\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u6309\u5404\u4e2a\u6761\u4ef6\u8fdb\u884c\u5bf9\u6bd4\u7684\uff0c\u5bf9\u4e8e\u4e0a\u9762\u6709\u7591\u95ee\u7684\u4e00\u884c\uff0c\u5176 Array \u4e2d\u7684 -5 \u662f\u5c0f\u4e8e 6 \u7684\uff0c\u800c 10 \u662f\u5927\u4e8e -1 \u7684\uff0c\u6240\u4ee5\u7cfb\u7edf\u4f1a\u8ba4\u4e3a\u8fd9\u4e00\u884c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u6761\u4ef6\uff08\u56e0\u4e3a\u7cfb\u7edf\u5185\u90e8\u4e0d\u7ba1\u662f\u4e0d\u662f Array \u7684\u540c\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u6761\u4ef6\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"> \u5982\u679c\u8981\u5728\u540c\u4e00\u4e2a\u5143\u7d20\u4e0a\u6ee1\u8db3\u5404\u4e2a\u6761\u4ef6\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 $elemMatch \u7b97\u7b26\uff0c\u6bd4\u5982\u6211\u4eec\u5c06\u4e0a\u9762\u7684\u67e5\u8be2\u8bed\u53e5\u505a\u4e00\u4e0b\u4fee\u6539\uff0c\u4f1a\u5f97\u5230\u4e0b\u9762\u7684\u7ed3\u679c\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&gt; db.foo.find({x: {$elemMatch: {$gt : -1, $lt : 6}}})\r\n{&quot;x&quot; : &#x5B;0, 5]}\r\n{&quot;x&quot; : &#x5B;-5, 5, 10]}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0a\u9762\u7ea2\u8272\u6709\u7591\u95ee\u7684\u90a3\u4e00\u6761\u6570\u636e\u5df2\u7ecf\u6ca1\u6709\u4e86\uff0c\u4f46\u662f\u7b49\u4e00\u4e0b\uff0c\u8fd8\u6709\u4e24\u6761\u6570\u636e\u4e5f\u8ddf\u7740\u6ca1\u6709\u4e86\uff0c\u8fd9\u662f\u4ec0\u4e48\u539f\u56e0\u5462\uff1f\u4e22\u5931\u7684\u4e24\u6761\u6570\u636e\u662f\u4e0b\u9762\u8fd9\u4e24\u6761\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{&quot;x&quot;: 0}\r\n{&quot;x&quot;: 5}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e24\u6761\u7684\u5171\u540c\u7279\u70b9\uff0c\u662f\u5176 x \u5bf9\u5e94\u7684 value \u503c\u5e76\u4e0d\u662f\u4e00\u4e2a Array\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u8fd9\u4e24\u6761\u4f1a\u4e22\u6389\u7684\u539f\u56e0\uff0c\u56e0\u4e3a $elemMatch \u7b97\u7b26\u53ea\u4f1a\u5bf9 Array \u8fdb\u884c\u7b5b\u9009\uff0c\u5982\u679c\u503c\u6839\u672c\u5c31\u4e0d\u662f\u4e00\u4e2a Array\uff0c\u4f1a\u88ab\u76f4\u63a5\u89c6\u4e3a\u4e0d\u6ee1\u8db3\u6761\u4ef6\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"> \u4e0a\u9762\u4e24\u4e2a\u67e5\u8be2\u65b9\u6cd5\uff0c\u6761\u6709\u6761\u7684\u539f\u7406\uff0c\u4f46\u662f\u90fd\u6ca1\u6709\u5f97\u5230\u9884\u671f\u7684\u7ed3\u679c\uff0c\u90a3\u6709\u6ca1\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u9884\u671f\u7684\u67e5\u8be2\u7ed3\u679c\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u8981\u7ed3\u5408 min() \u548c max() \u4e24\u4e2a\u65b9\u6cd5\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"> \u5148\u8bf4\u4e00\u4e0b min() \u548c max() \u4e24\u4e2a\u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4f5c\u7528\u4e8e MongoDB \u67e5\u8be2\u7684 cursor\uff0c\u5b83\u53ef\u4ee5\u5bf9 MongoDB \u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u7d22\u5f15\u7684\u65b9\u5f0f\u8fdb\u884c\u6307\u5bfc\uff0cmin(-1) \u7684\u610f\u601d\u662f\uff0cMongoDB \u5728\u4f7f\u7528\u6b64\u7d22\u5f15\u7684\u65f6\u5019\uff0c\u53ea\u4f7f\u7528\u5927\u4e8e -1 \u90e8\u5206\u7684\u7d22\u5f15\uff0c\u800c max(6) \u7684\u610f\u601d\u662f\uff0cMongoDB \u5728\u4f7f\u7528\u6b64\u7d22\u5f15\u7684\u65f6\u5019\uff0c\u53ea\u4f7f\u7528\u5c0f\u4e8e 6 \u90e8\u5206\u7684\u7d22\u5f15\u3002\u6bd4\u5982\u6211\u4eec\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\uff0c\u5c31\u80fd\u591f\u6210\u529f\u8fc7\u6ee4\u6389\u4e0a\u9762\u7ea2\u8272\u6709\u7591\u95ee\u7684\u4e00\u884c\u6570\u636e\u4e86\u3002<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&gt; db.foo.find({&quot;x&quot; : {&quot;$gt&quot; : -1, &quot;$lt&quot; : 6}}).min({&quot;x&quot; : -1}).max({&quot;x&quot; : 6})\r\n{&quot;x&quot; : 0}\r\n{&quot;x&quot; : &#x5B; 0, 5 ]}\r\n{&quot;x&quot; : 5}\r\n{&quot;x&quot; : &#x5B; -5, 5, 10 ]}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u539f\u6587\u53c2\u8003\uff1ahttp:\/\/www.kchodorow.com\/blog\/2012\/12\/27\/mongodb-puzzlers-1\/<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6765\u81ea MongoDB \u6838\u5fc3\u5f00\u53d1\u4eba\u5458@kchodorow \u7684\u535a\u6587\uff0c\u662f\u5176\u5173\u4e8e MongoDB \u67e5\u8be2\u8ff7\u9898\u7684\u7b2c\u4e00\u7bc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2457","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2457"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2457\/revisions"}],"predecessor-version":[{"id":2460,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2457\/revisions\/2460"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}