{"id":298896,"date":"2021-01-18T06:31:25","date_gmt":"2021-01-17T22:31:25","guid":{"rendered":"http:\/\/4563.org\/?p=298896"},"modified":"2021-01-18T06:31:25","modified_gmt":"2021-01-17T22:31:25","slug":"mongodb-%e7%9a%84-cursor-%e5%88%b0%e5%ba%95%e6%98%af%e6%80%8e%e4%b9%88%e5%b7%a5%e4%bd%9c%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=298896","title":{"rendered":"MongoDB \u7684 cursor \u5230\u5e95\u662f\u600e\u4e48\u5de5\u4f5c\u7684\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  MongoDB \u7684 cursor \u5230\u5e95\u662f\u600e\u4e48\u5de5\u4f5c\u7684\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u9996\u5148\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521d\u59cb\u5316 collection<\/p>\n<pre><code>db.collection.drop() for (i = 0; i &lt; 100; i++) {  db.collection.insertOne({x: Math.floor(i\/5)}); } <\/code><\/pre>\n<p>\u7136\u540e\u6267\u884c<code>var cursor = db.collection.find().batchSize(5).sort({x: 1})<\/code>\u83b7\u53d6\u5230 cursor\uff0c\u6267\u884c<code>cursor.next()<\/code>5 \u6b21\uff0c\u5728\u6267\u884c\u7b2c\u516d\u6b21\u4e4b\u524d\uff0c\u6267\u884c<code>db.collection.insertOne({x: 0})<\/code>\uff0c\u8fd9\u65f6\u6267\u884c<code>cursor.next()<\/code>\u7684\u7ed3\u679c\u4e3a<code>{x: 1}<\/code>\u3002<\/p>\n<h2>\u6211\u7684\u7591\u95ee<\/h2>\n<p>\u4e3a\u4ec0\u4e48<code>cursor.next()<\/code>\u7684\u7ed3\u679c\u4e0d\u662f<code>{x: 0}<\/code>\u5462\uff1f cursor \u5230\u5e95\u662f\u600e\u4e48\u5de5\u4f5c\u7684\uff1f\u5b83\u662f\u600e\u4e48\u51b3\u5b9a\u4e0b\u6b21 batch \u5e94\u8be5\u53d6\u54ea\u4e9b\u6570\u636e\u7684\u5462\uff1f<\/p>\n<h2>\u76f8\u5173\u8d44\u6599<\/h2>\n<ul>\n<li>What is a Cursor in MongoDB? &#8211; Stack Overflow<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5060690\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yagamil <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sort \u51fa\u6765\u7684\u662f\u4e00\u4e2a\u7d22\u5f15\u6570\u636e temp_index\uff0c\u6307\u5411\u7684\u4f60\u6392\u5e8f\u597d\u7684\u7684\u6570\u636e\uff0c\u4f60\u540e\u9762\u518d\u63d2\u5165\uff0c\u4e0d\u4f1a\u6539\u53d8\u4e4b\u524d sort \u7684 temp_index<br \/>\u6216\u8005\u7528\u4e00\u4e2a\u66f4\u52a0\u7b80\u5355\u7684\u4f8b\u5b50\uff0c \u5148 find(), \u7136\u540e\u63d2\u5165\uff0c\u518d\u7528 cursor next \u5230\u6700\u540e\uff0c\u770b\u4f1a\u4e0d\u4f1a\u627e\u5230\u4f60\u65b0\u63d2\u5165\u7684\u6570\u636e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5060691\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yagamil #1 <\/p>\n<p>1. \u6211\u600e\u4e48\u67e5\u770b temp_index \u5462\uff1f\u8fd9\u4e2a temp_index \u662f\u7279\u5b9a\u4e8e\u8fd9\u4e2a cursor \u7684\u5417\uff1f\u4f1a\u6709\u4e00\u4e2a\u7c7b\u4f3c offset \u7684\u4e1c\u897f\u8bb0\u5f55\u4e0b\u6b21\u5e94\u8be5\u8bfb\u53d6\u7684\u4f4d\u7f6e\u5417\uff1f\u6709\u6ca1\u6709\u76f8\u5173\u7684\u8d44\u6599\u8bb2\u8fd9\u4e2a\u7684\uff1f\u6211\u627e\u4e86\u597d\u4e45\u90fd\u6ca1\u6709\u627e\u5230\u3002<br \/>2. \u6211\u4e0d\u662f\u5f88\u7406\u89e3\u201c\u5148 find(), \u7136\u540e\u63d2\u5165\uff0c\u518d\u7528 cursor next \u5230\u6700\u540e\uff0c\u770b\u4f1a\u4e0d\u4f1a\u627e\u5230\u4f60\u65b0\u63d2\u5165\u7684\u6570\u636e\u201d\u3002\u80fd\u591f\u5177\u4f53\u89e3\u91ca\u4e00\u4e0b\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5060692\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yagamil #1 <\/p>\n<p>1. \u6211\u5728\u9644\u8a00 1 \u63cf\u8ff0\u4e86\u201c\u4f7f\u7528 sort \u548c\u6ca1\u6709\u4f7f\u7528 sort \u7684\u533a\u522b\u201d\u3002\u4f7f\u7528\u4e86 sort \u76f8\u5f53\u4e8e\u9700\u8981\u5728\u5185\u5b58\u4e34\u65f6\u5b58\u50a8\u6574\u4e2a\u7ed3\u679c\u96c6\u5417\uff1f\u90a3\u4e48\u4e0d\u662f\u5b8c\u5168\u8fdd\u80cc\u4e86\u4f7f\u7528 cursor \u7684\u521d\u8877\uff1f<br \/>2. \u5728\u6ca1\u6709\u4f7f\u7528 sort \u65f6\uff0c\u5b83\u662f\u600e\u4e48\u51b3\u5b9a\u4e0b\u6b21 batch \u5e94\u8be5\u83b7\u53d6\u5230\u54ea\u4e9b\u6570\u636e\u7684\u5462\uff1f\u5728 primary index \u4e0a\u8bb0\u5f55 cursor \u7684 offset \u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5060693\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leopod1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &gt; 1. \u6211\u5728\u9644\u8a00 1 \u63cf\u8ff0\u4e86\u201c\u4f7f\u7528 sort \u548c\u6ca1\u6709\u4f7f\u7528 sort \u7684\u533a\u522b\u201d\u3002\u4f7f\u7528\u4e86 sort \u76f8\u5f53\u4e8e\u9700\u8981\u5728\u5185\u5b58\u4e34\u65f6\u5b58\u50a8\u6574\u4e2a\u7ed3\u679c\u96c6\u5417\uff1f\u90a3\u4e48\u4e0d\u662f\u5b8c\u5168\u8fdd\u80cc\u4e86\u4f7f\u7528 cursor \u7684\u521d\u8877\uff1f<\/p>\n<p>\u56e0\u4e3a\u4f60 sort \u7684 key \u662f x \u662f\u6ca1\u6709\u5efa\u7acb\u7d22\u5f15\u7684\uff0c\u6240\u4ee5\u624d\u4f1a\u9700\u8981\u5728\u5185\u5b58\u8fdb\u884c\u6392\u5e8f\u3002<br \/>\u5982\u4f55\u590d\u73b0-&gt; \u7ed9 x \u52a0\u4e0a\u7d22\u5f15\uff0csort \u7684\u884c\u4e3a\u4f1a\u4fdd\u6301\u4e00\u81f4\u3002<br \/>&gt; 2. \u5728\u6ca1\u6709\u4f7f\u7528 sort \u65f6\uff0c\u5b83\u662f\u600e\u4e48\u51b3\u5b9a\u4e0b\u6b21 batch \u5e94\u8be5\u83b7\u53d6\u5230\u54ea\u4e9b\u6570\u636e\u7684\u5462\uff1f\u5728 primary index \u4e0a\u8bb0\u5f55 cursor \u7684 offset \u5417\uff1f<br \/>\u6ca1\u6709\u4f7f\u7528 sort\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684 query \u3002\u5982\u679c\u662f\u4f8b\u5b50\u4e2d\u7684 sql,\u4f1a\u9ed8\u8ba4\u5168\u8868\u626b\uff0c\u4e5f\u5c31\u662f\u6839\u636e_id \u3002<\/p>\n<p>### \u603b\u7ed3<br \/>\u4f60\u7684\u95ee\u9898\u4e3b\u8981\u662f\u4e0d\u77e5\u9053 Mongodb \u7684\u67e5\u8be2\u539f\u7406\uff0c\u5efa\u8bae\u591a\u5b66\u4e60\u67e5\u8be2\u8ba1\u5212,\u6216\u8005\u591a\u770b\u6e90\u7801\u3002<br \/>mongodb \u6709\u4e2a\u5f88\u91cd\u8981\u7684\u6982\u5ff5\u53eb\u505a stage\uff0c`query`,&#8217;sort&#8217;,&#8217;fetch&#8217;\u90fd\u53ea\u662f\u67e5\u8be2\u91cc\u9762\u7684\u4e00\u4e2a stage                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5060694\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @leopod1995 #4 \u8c22\u8c22\u4f60\u7684\u56de\u590d\u3002\u731c\u5230\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff0c\u4e0d\u8fc7\u8fd8\u6ca1\u6709\u770b\u5230\u67e5\u8be2\u8ba1\u5212\u90a3\u5757\uff0c\u63a5\u4e0b\u6765\u770b\u770b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5060695\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : muzuiget <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684\u4ee3\u7801\u6709\u4e24\u4e2a\u95ee\u9898\u3002\u524d\u63d0\u662f\u4f60\u8981\u77e5\u9053 MongoDB \u7684\u6570\u636e\u662f\u5728\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u91cc\u7684\uff0c\u5bf9\u4f60\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u5b83\u5c31\u662f\u4e00\u4e2a\u670d\u52a1\u5668\u3002<\/p>\n<p>\u56de\u5230 cursor \u95ee\u9898\uff0ccursor \u53ef\u4ee5\u662f\u4e00\u201c\u4f1a\u72b6\u6001\u4e3a\u4e86\u6709\u4e2a\u73b0\u5b9e\u4f8b\u5b50\uff0c\u5feb\u9012\u7ad9\u8ddf\u4f60\u8bf4\u4f60\u6709 10 \u4e2a\u5305\u8981\u6536\uff0c\u4f46\u662f\u4f60\u5bb6\u91cc\u53ea\u80fd\u5b58\u653e 1 \u4e2a\u5305\u4e86\uff0c\u4e0d\u80fd\u4e00\u6b21\u6536\u4e0b\uff0c\u7b49\u4f60\u5148\u5904\u7406\u4e00\u4e2a\u5305\uff0c\u7136\u540e\u518d\u6536\u4e0b\u4e00\u4e2a\uff0ccursor.next() \u5c31\u662f\u670d\u52a1\u5668\u5411\u4f60\u53d1\u4e0b\u4e00\u6761\u8bb0\u5f55\u7684\u610f\u601d\u3002\u518d\u5047\u8bbe\u4f60\u5904\u7406\u4e86\u4e24\u4e2a\u5305\u540e\uff0c\u4f59\u4e0b\u7684 8 \u4e2a\u5305\u4e0d\u60f3\u8981\u4e86\uff0c\u76f4\u63a5\u9000\u8d27\uff0c\u90a3\u4e48\u5feb\u9012\u7ad9\u4e0d\u4f1a\u518d\u7ed9\u4f60\u6d3e\u9001\u4e86\uff0c\u7c7b\u4f3c\u6709 cursor.close() \u4f59\u4e0b\u7684\u6761\u76ee\u4e0d\u8981\u4e86\u3002\u5982\u679c\u4f60\u7684\u5185\u5b58\u8db3\u591f\u5927\uff0c\u80fd\u6709\u4e00\u6b21\u8fc7\u6536\u53d1\u6240\u6709\u8bb0\u5f55\uff0c\u90a3 cursor \u4e5f\u6709 toArray() \u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\uff0c\u4f60\u7684\u6d4b\u8bd5\u4ee3\u7801\uff0c\u4f60\u7528 await \u4e86\u5417\uff0c\u636e\u6211\u6240\u77e5\uff0cMongoDB \u7684 NodeJS \u63a5\u53e3\u90fd\u662f\u5f02\u6b65\u4e86\uff0c\u8c03\u7528\u51fd\u6570\u540e\uff0c\u672a\u5fc5\u9a6c\u4e0a\u6267\u884c\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u518d\u8bf4\uff0c\u679a\u4e3e\u4e00\u4e2a\u6570\u7ec4\u7c7b\u578b\u7684\u5bb9\u5668\u8fc7\u7a0b\u65f6\uff0c\u5c31\u4e0d\u5e94\u8be5\u540c\u65f6\u4fee\u6539\u8be5\u5bf9\u8c61\u7684\u5143\u7d20\u957f\u5ea6\uff0c\u8fd9\u6837\u592a\u5bb9\u6613\u51fa\u73b0\u4e0d\u53ef\u9884\u6d4b\u7684\u72b6\u6001\u9519\u8bef\uff0c\u4f60\u8981\u4ed4\u7ec6\u770b\u76f8\u5173\u6587\u6863\u770b\u770b\u8fd9\u6837\u505a\u662f\u5426\u5b89\u5168\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u7684 cursor &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/298896"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=298896"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/298896\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298896"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}