{"id":1092,"date":"2023-03-12T11:28:28","date_gmt":"2023-03-12T03:28:28","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1092"},"modified":"2023-04-29T11:28:10","modified_gmt":"2023-04-29T03:28:10","slug":"mongodb-limit-and-skip","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-limit-and-skip\/","title":{"rendered":"MongoDB Limit\u4e0eSkip\u65b9\u6cd5"},"content":{"rendered":"<h2>MongoDB Limit() \u65b9\u6cd5<\/h2>\n<p>\u5982\u679c\u9700\u8981\u5728MongoDB\u4e2d\u8bfb\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u6570\u636e\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528MongoDB\u7684Limit\u65b9\u6cd5\uff0climit()\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u6570\u5b57\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u6307\u5b9a\u4eceMongoDB\u4e2d\u8bfb\u53d6\u7684\u8bb0\u5f55\u6761\u6570\u3002<\/p>\n<p><!-- more --><\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>limit()\u65b9\u6cd5\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.COLLECTION_NAME.find().limit(NUMBER)<\/code><\/pre>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u96c6\u5408 col \u4e2d\u7684\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.find()\n{ &quot;_id&quot; : ObjectId(&quot;5aefe63c2d2577acadf1e0d2&quot;), &quot;title&quot; : &quot;PHP&quot;, &quot;description&quot; : &quot;PHP \u662f\u4e00\u79cd\u521b\u5efa\u52a8\u6001\u4ea4\u4e92\u6027\u7ad9\u70b9\u7684\u5f3a\u6709\u529b\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\u3002&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }\n{ &quot;_id&quot; : ObjectId(&quot;5aefe6462d2577acadf1e0d3&quot;), &quot;title&quot; : &quot;Java&quot;, &quot;description&quot; : &quot;Java \u662f\u7531Sun\u516c\u53f8\u4e8e1995\u5e745\u6708\u63a8\u51fa\u7684\u9ad8\u7ea7\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u3002&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }\n{ &quot;_id&quot; : ObjectId(&quot;5aefe64f2d2577acadf1e0d4&quot;), &quot;title&quot; : &quot;MongoDB&quot;, &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u4e3a\u663e\u793a\u67e5\u8be2\u6587\u6863\u4e2d\u7684\u4e24\u6761\u8bb0\u5f55\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.find({},{&quot;title&quot;:1,_id:0}).limit(2)\n{ &quot;title&quot; : &quot;PHP&quot; }\n{ &quot;title&quot; : &quot;Java&quot; }<\/code><\/pre>\n<p>\u6ce8\uff1a\u5982\u679c\u4f60\u4eec\u6ca1\u6709\u6307\u5b9alimit()\u65b9\u6cd5\u4e2d\u7684\u53c2\u6570\u5219\u663e\u793a\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<h2>MongoDB Skip() \u65b9\u6cd5<\/h2>\n<p>\u9664\u4e86\u53ef\u4ee5\u4f7f\u7528limit()\u65b9\u6cd5\u6765\u8bfb\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u6570\u636e\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528skip()\u65b9\u6cd5\u6765\u8df3\u8fc7\u6307\u5b9a\u6570\u91cf\u7684\u6570\u636e\uff0cskip()\u65b9\u6cd5\u540c\u6837\u63a5\u53d7\u4e00\u4e2a\u6570\u5b57\u53c2\u6570\u4f5c\u4e3a\u8df3\u8fc7\u7684\u8bb0\u5f55\u6761\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>skip() \u65b9\u6cd5\u811a\u672c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)<\/code><\/pre>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u53ea\u4f1a\u663e\u793a\u7b2c\u4e8c\u6761\u6587\u6863\u6570\u636e<\/p>\n<pre><code class=\"language-javascript\">> db.col.find({},{&quot;title&quot;:1,_id:0}).limit(1).skip(1)\n{ &quot;title&quot; : &quot;Java&quot; }<\/code><\/pre>\n<p>\u6ce8\uff1askip()\u65b9\u6cd5\u9ed8\u8ba4\u53c2\u6570\u4e3a 0<\/p>\n<blockquote>\n<p>\u5f53\u67e5\u8be2\u65f6\u540c\u65f6\u4f7f\u7528sort,skip,limit\uff0c\u65e0\u8bba\u4f4d\u7f6e\u5148\u540e\uff0c\u6267\u884c\u987a\u5e8f\u4e3a\uff1asort-&gt;skip-&gt;limit\u3002<\/p>\n<p>\u4e0d\u8981\u8f7b\u6613\u4f7f\u7528skip\u6765\u505a\u67e5\u8be2\uff0c\u5426\u5219\u6570\u636e\u91cf\u5927\u4e86\u5c31\u4f1a\u5bfc\u81f4\u6027\u80fd\u6025\u5267\u4e0b\u964d\uff0c\u8fd9\u662f\u56e0\u4e3aSkip\u662f\u4e00\u6761\u4e00\u6761\u7684\u6570\u8fc7\u6765\u7684<\/p>\n<\/blockquote>\n<p>\u5047\u8bbe\u6211\u4eec\u67e5\u8be2\u7b2c100001\u6761\u6570\u636e\uff0c\u8fd9\u6761\u6570\u636e\u7684Amount\u503c\u662f\uff1a2399928\uff0c\u6211\u4eec\u6765\u5199\u4e24\u6761\u8bed\u53e5\u5206\u522b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.test.sort({&quot;amount&quot;:1}).skip(100000).limit(10)  \/\/183ms\ndb.test.find({amount:{$gt:2399928}}).sort({&quot;amount&quot;:1}).limit(10)  \/\/53ms<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB Limit() \u65b9\u6cd5 \u5982\u679c\u9700\u8981\u5728MongoDB\u4e2d\u8bfb\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u6570\u636e\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528MongoDB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300],"tags":[],"class_list":["post-1092","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1092"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1092\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}