{"id":5324,"date":"2022-03-20T12:08:50","date_gmt":"2022-03-20T04:08:50","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5324"},"modified":"2022-03-20T12:08:50","modified_gmt":"2022-03-20T04:08:50","slug":"mongodb-%e7%9a%84%e6%b5%b7%e9%87%8f%e6%95%b0%e6%8d%ae%e5%88%86%e9%a1%b5%e6%9f%a5%e8%af%a2%e4%bc%98%e5%8c%96%e5%ae%9e%e6%88%98%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5324","title":{"rendered":"Mongodb \u7684\u6d77\u91cf\u6570\u636e\u5206\u9875\u67e5\u8be2\u4f18\u5316\u5b9e\u6218\uff01"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u57fa\u4e8e\u6211\u4eec\u7684\u6570\u636e\u7279\u6027\uff0c\u5728\u8fdb\u884c\u6570\u636e\u5e93\u9009\u578b\u65f6\u9009\u62e9\u4e86 mongo \u6570\u636e\u5e93\u3002\u5728\u6587\u6863\u6570\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u5b58\u5728\u6162\u67e5\u8be2\uff0c\u5f71\u54cd\u670d\u52a1\u7aef\u6027\u80fd\u3002\u5408\u7406\u5730\u5bf9\u6570\u636e\u5e93\u547d\u4ee4\u53ca\u7d22\u5f15\u8fdb\u884c\u4f18\u5316\uff0c\u53ef\u4ee5\u5f88\u5927\u5e45\u5ea6\u63d0\u5347\u63a5\u53e3\u6027\u80fd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/mongo0.png\" alt=\"mongo \u5206\u9875\u67e5\u8be2\" \/><figcaption>mongo \u5206\u9875\u67e5\u8be2<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">mongo \u5206\u9875\u67e5\u8be2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Java \u4e2d\u4f7f\u7528 mongodb \u7684 MongoTemplate \u8fdb\u884c\u5206\u9875\u65f6\uff0c\u4e00\u822c\u7684\u7b56\u7565\u662f\u4f7f\u7528 skip+limit \u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u7565\u8fc7\u5927\u91cf\u6570\u636e\u7684\u65f6\u5019\u5c31\u663e\u5f97\u5f88\u4f4e\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f20\u7edf\u5206\u9875\u4ecb\u7ecd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4e00\u9875\u5927\u5c0f\u4e3a 10 \u6761\u3002\u5219\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/page 1\n1-10\n\n\/\/page 2\n11-20\n\n\/\/page 3\n21-30\n...\n\n\/\/page n\n10*(n-1)+1-10*n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \u63d0\u4f9b\u4e86 skip() \u548c limit() \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">skip: \u8df3\u8fc7\u6307\u5b9a\u6570\u91cf\u7684\u6570\u636e\u3002\u53ef\u4ee5\u7528\u6765\u8df3\u8fc7\u5f53\u524d\u9875\u4e4b\u524d\u7684\u6570\u636e\uff0c\u5373\u8df3\u8fc7 <code>pageSize*(n-1)<\/code>\u3002limit: \u6307\u5b9a\u4ece MongoDB \u4e2d\u8bfb\u53d6\u7684\u8bb0\u5f55\u6761\u6570\uff0c\u53ef\u4ee5\u5f53\u505a\u9875\u9762\u5927\u5c0f pageSize\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u5206\u9875\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Page 1\ndb.getCollection('file').find({}).limit(10)\n\n\/\/Page 2\ndb.getCollection('file').find({}).skip(10).limit(10)\n\n\/\/Page 3\ndb.getCollection('file').find({}).skip(20).limit(10)\n........<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b58\u5728\u95ee\u9898<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u6587\u6863\u5bf9 skip \u7684\u63cf\u8ff0\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">skip \u65b9\u6cd5\u4ece\u7ed3\u679c\u96c6\u7684\u5f00\u5934\u8fdb\u884c\u626b\u63cf\u540e\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u3002\u8fd9\u6837\u968f\u7740\u504f\u79fb\u7684\u589e\u52a0\uff0cskip \u5c06\u53d8\u5f97\u66f4\u6162\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u275d<\/strong><\/p><p>The cursor.skip() method requires the server to scan from the beginning of the input results set before beginning to return results. As the offset increases, cursor.skip() will become slower.\u275e<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u9700\u8981\u4e00\u79cd\u66f4\u5feb\u7684\u65b9\u5f0f\u3002\u5176\u5b9e\u548c mysql \u6570\u91cf\u5927\u4e4b\u540e\u4e0d\u63a8\u8350\u7528 limit m,n \u4e00\u6837\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u5efa\u8bae\u4f7f\u7528\u8303\u56f4\u67e5\u8be2\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[\u7d22\u5f15]<\/code>\u5206\u9875\u76f8\u6bd4\uff0c\u504f\u79fb\u91cf\u589e\u52a0\u65f6\u901a\u5e38\u4f1a\u4ea7\u751f\u66f4\u597d\u7684\u6027\u80fd\u3002\u5373\u6307\u5b9a\u5f00\u59cb\u4f4d\u7f6e\u89e3\u51b3\u65b9\u6848\u662f\u5148\u67e5\u51fa\u5f53\u524d\u9875\u7684\u7b2c\u4e00\u6761\uff0c\u7136\u540e\u987a\u5e8f\u6570 pageSize \u6761\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6307\u5b9a\u8303\u56f4\u5206\u9875\u4ecb\u7ecd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5047\u8bbe\u57fa\u4e8e<code>_id<\/code>\u7684\u6761\u4ef6\u8fdb\u884c\u67e5\u8be2\u6bd4\u8f83\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2a\u6bd4\u8f83\u7684\u57fa\u51c6\u5b57\u6bb5\u53ef\u4ee5\u662f\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u6709\u5e8f\u7684\u5b57\u6bb5\uff0c\u6bd4\u5982\u65f6\u95f4\u6233\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Page 1\ndb.getCollection('file').find({}).limit(pageSize);\n\/\/Find the id of the last document in this page\nlast_id =...\n\n\/\/Page 2\nusers =db.getCollection('file').find({\n'_id':{\"$gt\":ObjectId(\"5b16c194666cd10add402c87\")}\n}).limit(10)\n\n\/\/Update the last id with the id of the last document in this page\nlast_id =...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u663e\u7136\uff0c\u7b2c\u4e00\u9875\u548c\u540e\u9762\u7684\u4e0d\u540c\u3002\u5bf9\u4e8e\u6784\u5efa\u5206\u9875 API\uff0c\u6211\u4eec\u53ef\u4ee5\u8981\u6c42\u7528\u6237\u5fc5\u987b\u4f20\u9012 pageSize\uff0clastId\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cpageSize\u300d<\/strong> \u9875\u9762\u5927\u5c0f<br>\n<strong>\u300clastId\u300d<\/strong> \u4e0a\u4e00\u9875\u7684\u6700\u540e\u4e00\u6761\u8bb0\u5f55\u7684 id\uff0c\u5982\u679c\u4e0d\u4f20\uff0c\u5219\u5c06\u5f3a\u5236\u4e3a\u7b2c\u4e00\u9875\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u964d\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>_id<\/code>\u964d\u5e8f\uff0c\u7b2c\u4e00\u9875\u662f\u6700\u5927\u7684\uff0c\u4e0b\u4e00\u9875\u7684 id \u6bd4\u4e0a\u4e00\u9875\u7684\u6700\u540e\u7684 id \u8fd8\u5c0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find({ _id:{ $lt:lastId}})\n.sort({ _id:-1})\n.limit(pageSize)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5347\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>_id<\/code>\u5347\u5e8f\uff0c\u4e0b\u4e00\u9875\u7684 id \u6bd4\u4e0a\u4e00\u9875\u7684\u6700\u540e\u4e00\u6761\u8bb0\u5f55 id \u8fd8\u5927\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find({ _id:{ $gt:lastId}})\n.sort({ _id:1})\n.limit(pageSize )<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u6761\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u4e00\u5171\u591a\u5c11\u6761\u548c\u591a\u5c11\u9875\u7684\u95ee\u9898\u3002\u6240\u4ee5\uff0c\u9700\u8981\u5148\u67e5\u4e00\u5171\u591a\u5c11\u6761 count\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find({}).count();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">ObjectId \u7684\u6709\u5e8f\u6027\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u770b ObjectId \u751f\u6210\u89c4\u5219\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982<code>\"_id\" : ObjectId(\"5b1886f8965c44c78540a4fc\")<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d6 id \u7684\u524d 4 \u4e2a\u5b57\u8282\u3002\u7531\u4e8e id \u662f 16 \u8fdb\u5236\u7684 string\uff0c4 \u4e2a\u5b57\u8282\u5c31\u662f 32 \u4f4d\uff0c\u5bf9\u5e94 id \u524d 8 \u4e2a\u5b57\u7b26\u3002\u5373 5b1886f8, \u8f6c\u6362\u6210 10 \u8fdb\u5236\u4e3a 1528334072. \u52a0\u4e0a 1970\uff0c\u5c31\u662f\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8b\u5b9e\u4e0a\uff0c\u66f4\u7b80\u5355\u7684\u529e\u6cd5\u662f\u67e5\u770b org.mongodb:bson:3.4.3 \u91cc\u7684 ObjectId \u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>publicObjectId(Date date){\n        this(dateToTimestampSeconds(date), MACHINE_IDENTIFIER, PROCESS_IDENTIFIER, NEXT_COUNTER.getAndIncrement(),false);\n}\n\n\/\/org.bson.types.ObjectId#dateToTimestampSeconds \nprivatestatic int dateToTimestampSeconds(Date time){\n    return(int)(time.getTime()\/ 1000L);\n}\n\n\/\/java.util.Date#getTime\n\/**\n * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT\n * represented by this &lt;tt&gt;Date&lt;\/tt&gt; object.\n *\n * @return  the number of milliseconds since January 1, 1970, 00:00:00 GMT\n *          represented by this date.\n *\/\npublic long getTime(){\n    returngetTimeImpl();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \u7684 ObjectId \u5e94\u8be5\u662f\u968f\u7740\u65f6\u95f4\u800c\u589e\u52a0\u7684\uff0c\u5373\u540e\u63d2\u5165\u7684 id \u4f1a\u6bd4\u4e4b\u524d\u7684\u5927\u3002\u4f46\u8003\u91cf id \u7684\u751f\u6210\u89c4\u5219\uff0c\u6700\u5c0f\u65f6\u95f4\u6392\u5e8f\u533a\u5206\u662f\u79d2\uff0c\u540c\u4e00\u79d2\u5185\u7684\u6392\u5e8f\u65e0\u6cd5\u4fdd\u8bc1\u3002\u5f53\u7136\uff0c\u5982\u679c\u662f\u540c\u4e00\u53f0\u673a\u5668\u7684\u540c\u4e00\u4e2a\u8fdb\u7a0b\u751f\u6210\u7684\u5bf9\u8c61\uff0c\u662f\u6709\u5e8f\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u662f\u5206\u5e03\u5f0f\u673a\u5668\uff0c\u4e0d\u540c\u673a\u5668\u65f6\u949f\u540c\u6b65\u548c\u504f\u79fb\u7684\u95ee\u9898\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u6709\u4e2a\u5b57\u6bb5\u53ef\u4ee5\u4fdd\u8bc1\u662f\u6709\u5e8f\u7684\uff0c\u90a3\u4e48\u7528\u8fd9\u4e2a\u5b57\u6bb5\u6765\u6392\u5e8f\u662f\u6700\u597d\u7684\u3002<code>_id<\/code>\u5219\u662f\u6700\u540e\u7684\u5907\u9009\u65b9\u6848\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b58\u5728\u95ee\u9898<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u5206\u9875\u770b\u8d77\u6765\u770b\u7406\u60f3\uff0c\u867d\u7136\u786e\u5b9e\u662f\uff0c\u4f46\u6709\u4e2a\u95ee\u9898\u662f\u4e0d\u80fd\u65e0\u6cd5\u505a\u5230\u8df3\u9875\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u7684\u5206\u9875\u6570\u636e\u8981\u548c\u6392\u5e8f\u952e\u5173\u8054\uff0c\u6240\u4ee5\u5fc5\u987b\u6709\u4e00\u4e2a\u6392\u5e8f\u57fa\u51c6\u6765\u622a\u65ad\u8bb0\u5f55\u3002\u800c\u8df3\u9875\uff0c\u6211\u53ea\u77e5\u9053\u7b2c\u51e0\u9875\uff0c\u6761\u4ef6\u4e0d\u8db3\uff0c\u65e0\u6cd5\u5206\u9875\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5b9e\u4e1a\u52a1\u9700\u6c42\u786e\u5b9e\u63d0\u51fa\u4e86\u8df3\u9875\u7684\u9700\u6c42\uff0c\u867d\u7136\u51e0\u4e4e\u4e0d\u4f1a\u6709\u4eba\u7528\uff0c\u4eba\u4eec\u66f4\u5173\u5fc3\u7684\u662f\u5f00\u5934\u548c\u7ed3\u5c3e\uff0c\u800c\u7ed3\u5c3e\u53ef\u4ee5\u901a\u8fc7\u9006\u6392\u5e8f\u7684\u65b9\u6848\u8f6c\u6210\u5f00\u5934\u3002\u6240\u4ee5\uff0c\u771f\u6b63\u5206\u9875\u7684\u9700\u6c42\u5e94\u5f53\u662f\u4e0d\u5b58\u5728\u7684\u3002\u5982\u679c\u4f60\u662f\u4e3a\u4e86\u67e5\u627e\u67d0\u4e2a\u8bb0\u5f55\uff0c\u90a3\u4e48\u67e5\u8be2\u6761\u4ef6\u641c\u7d22\u662f\u6700\u5feb\u7684\u65b9\u6848\u3002\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u67e5\u8be2\u6761\u4ef6\uff0c\u901a\u8fc7\u8089\u773c\u53bb\u4e00\u4e00\u67e5\u770b\uff0c\u90a3\u4e48\u4e0b\u4e00\u9875\u8db3\u77e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u5c31\u662f\u60f3\u626d\u8f6c\u4f20\u7edf\u5206\u9875\u7684\u6982\u5ff5\uff0c\u5728\u4e92\u8054\u7f51\u53d1\u5c55\u7684\u4eca\u5929\uff0c\u5927\u90e8\u5206\u6570\u636e\u7684\u4f53\u91cf\u90fd\u662f\u5e9e\u5927\u7684\uff0c\u8df3\u9875\u7684\u9700\u6c42\u5c06\u6d88\u8017\u66f4\u591a\u7684\u5185\u5b58\u548c cpu\uff0c\u5bf9\u5e94\u7684\u5c31\u662f\u67e5\u8be2\u6162\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\uff0c\u5982\u679c\u6570\u91cf\u4e0d\u5927\uff0c\u5982\u679c\u4e0d\u4ecb\u610f\u6162\u4e00\u70b9\uff0c\u90a3\u4e48 skip \u4e5f\u4e0d\u662f\u5565\u95ee\u9898\uff0c\u5173\u952e\u8981\u770b\u4e1a\u52a1\u573a\u666f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eca\u5929\u63a5\u5230\u7684\u9700\u6c42\u5c31\u662f\u8981\u8df3\u9875\uff0c\u800c\u4e14\u6570\u91cf\u5f88\u5c0f\uff0c\u90a3\u4e48 skip \u5427\uff0c\u4e0d\u8d39\u4e8b\uff0c\u8fd8\u5feb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982 google\uff0c\u770b\u8d77\u6765\u662f\u6709\u8df3\u9875\u9009\u62e9\u7684\u554a\u3002\u518d\u4ed4\u7ec6\u770b\uff0c\u53ea\u6709 10 \u9875\uff0c\u591a\u7684\u5c31\u5fc5\u987b\u4e0b\u4e00\u9875\uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u5171\u591a\u5c11\u9875\uff0c\u8df3\u5230\u4efb\u610f\u9875\u7684\u9009\u62e9\u3002\u8fd9\u4e0d\u5c31\u662f\u6211\u4eec\u7684 find-condition-then-limit \u65b9\u6848\u5417\uff0c\u53ea\u662f\u4ed6\u7684\u4e00\u9875\u6570\u91cf\u6bd4\u8f83\u591a\uff0c\u524d\u7aef\u6216\u8005\u540e\u7aef\u628a\u8fd9\u4e00\u9875\u7ed9\u5207\u6210\u4e86 10 \u4efd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/mongo1.png\" alt=\"\u4e0d\u80fd\u8df3\u9875\u7684\u5206\u9875\" \/><figcaption>\u4e0d\u80fd\u8df3\u9875\u7684\u5206\u9875<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u6837\uff0cFacebook\uff0c\u867d\u7136\u63d0\u4f9b\u4e86\u603b count\uff0c\u4f46\u4e5f\u53ea\u80fd\u4e0b\u4e00\u9875\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2022\/03\/mongo2.png\" alt=\"\u4e0d\u63d0\u4f9b\u8df3\u9875\u7684\u5206\u9875\u5217\u8868\" \/><figcaption>\u4e0d\u63d0\u4f9b\u8df3\u9875\u7684\u5206\u9875\u5217\u8868<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4ed6\u573a\u666f\uff0c\u6bd4\u5982 Twitter\uff0c\u5fae\u535a\uff0c\u670b\u53cb\u5708\u7b49\uff0c\u6839\u672c\u6ca1\u6709\u8df3\u9875\u7684\u6982\u5ff5\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u786e\u5b9e\u6709\u8df3\u9875\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4ecd\u65e7\u91c7\u7528 skip \u505a\u5206\u9875\uff0c\u76ee\u524d\u8fd8\u6ca1\u6709\u53d1\u73b0\u6027\u80fd\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private List&lt;DBObject&gt; doFindItems(String collectionName,\n      Map&lt;String, Object&gt; query, DBObject showFields, int skip,\n      int limit, DBObject order) {\n\n   List&lt;DBObject&gt; result = null;\n   DBObject obj = genDBObject(query);\n   DBCursor cursor = readDB.getCollection(collectionName)\n         .find(obj, showFields);\n   if (cursor != null) {\n      try {\n         if (order != null) {\n            cursor.sort(order);\n         }\n         cursor.skip(skip).limit(limit);\n         result = cursor.toArray();\n      } finally {\n         cursor.close();\n      }\n   }\n  return result;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6392\u5e8f\u548c\u6027\u80fd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u9762\u5173\u6ce8\u4e8e\u5206\u9875\u7684\u5b9e\u73b0\u539f\u7406\uff0c\u4f46\u5ffd\u7565\u4e86\u6392\u5e8f\u3002\u65e2\u7136\u5206\u9875\uff0c\u80af\u5b9a\u662f\u6309\u7167\u67d0\u4e2a\u987a\u5e8f\u8fdb\u884c\u5206\u9875\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u8981\u6709\u6392\u5e8f\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \u7684 sort \u548c find \u7ec4\u5408<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find().sort({'createTime':1}).limit(5)\ndb.getCollection('file').find().limit(5).sort({'createTime':1})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e24\u4e2a\u90fd\u662f\u7b49\u4ef7\u7684\uff0c\u987a\u5e8f\u4e0d\u5f71\u54cd\u6267\u884c\u987a\u5e8f\u3002\u5373\uff0c\u90fd\u662f\u5148 find \u67e5\u8be2\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c\uff0c\u7136\u540e\u5728\u7ed3\u679c\u96c6\u4e2d\u6392\u5e8f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u6761\u4ef6\u67e5\u8be2\u6709\u65f6\u5019\u4e5f\u4f1a\u6309\u7167\u67d0\u5b57\u6bb5\u6392\u5e8f\u7684\uff0c\u6bd4\u5982\u6309\u7167\u65f6\u95f4\u6392\u5e8f\u3002\u67e5\u8be2\u4e00\u7ec4\u65f6\u95f4\u5e8f\u5217\u7684\u6570\u636e\uff0c\u6211\u4eec\u60f3\u8981\u6309\u7167\u65f6\u95f4\u5148\u540e\u987a\u5e8f\u6765\u663e\u793a\u5185\u5bb9\uff0c\u5219\u5fc5\u987b\u5148\u6309\u7167\u65f6\u95f4\u5b57\u6bb5\u6392\u5e8f\uff0c\u7136\u540e\u518d\u6309\u7167 id \u5347\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find({productId:5}).sort({createTime:1, _id:1}).limit(5)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5148\u6309\u7167 createTime \u5347\u5e8f\uff0c\u7136\u540e createTime \u76f8\u540c\u7684 record \u518d\u6309\u7167_id \u5347\u5e8f\uff0c\u5982\u6b64\u53ef\u4ee5\u5b9e\u73b0\u6211\u4eec\u7684\u5206\u9875\u529f\u80fd\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u5b57\u6bb5\u6392\u5e8f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').sort({taskRole:1,appId:-1})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8868\u793a\u5148\u6309\u7167 taskRole \u5347\u5e8f\uff0c\u518d\u6309 appId \u964d\u5e8f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find({});\n\n\/* \u7ed3\u679c\uff1a*\/\n\/* 1 *\/\n{\n    \"_id\" : ObjectId(\"5e7179de0af8595d0bbe243f\"),\n    \"fileName\" : \"test.apk\",\n    \"fileCTime\" : NumberLong(1584495748123),\n    \"version\" : \"1\"\n}\n\/* 2 *\/\n{\n    \"_id\" : ObjectId(\"5e7dc423e20bc4b7fa6c205a\"),\n\"fileName\" : \"b.html\",\n\"version\" : \"2\"\n}\n\/* 3 *\/\n{\n    \"_id\" : ObjectId(\"5e7dc423e20bc4b7fa6c205b\"),\n\"fileName\" : \"b.html\",\n\"version\" : \"3\"\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6309\u7167 fileName \u5347\u5e8f\uff0c\u7136\u540e\u6309\u7167 version \u964d\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.getCollection('file').find({}).sort({fileName:1,version:-1})\n\n\/*\u7ed3\u679c\uff1a*\/\n\/* 1 *\/\n{\n    \"_id\" : ObjectId(\"5e7dc423e20bc4b7fa6c205a\"),\n\"fileName\" : \"b.html\",\n\"version\" : \"2 \"\n}\n\/* 2 *\/\n{\n    \"_id\" : ObjectId(\"5e7dc423e20bc4b7fa6c205b\"),\n\"fileName\" : \" b.html\",\n\"version\" : \"3\"\n}\n\/* 3 *\/\n{\n    \"_id\" : ObjectId(\"5e7179de0af8595d0bbe243f\"),\n    \"fileName\" : \"test.apk\",\n    \"fileCTime\" : NumberLong(1584495748123),\n    \"version\" : \"1\"\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u5185\u5bb9\u5e0c\u671b\u80fd\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5324\">Mongodb \u7684\u6d77\u91cf\u6570\u636e\u5206\u9875\u67e5\u8be2\u4f18\u5316\u5b9e\u6218\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8e\u6211\u4eec\u7684\u6570\u636e\u7279\u6027\uff0c\u5728\u8fdb\u884c\u6570\u636e\u5e93\u9009\u578b\u65f6\u9009\u62e9\u4e86 mongo \u6570\u636e\u5e93\u3002\u5728\u6587\u6863\u6570\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u5b58\u5728\u6162\u67e5\u8be2\uff0c\u5f71\u54cd\u670d\u52a1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1774,1767,2046,1578,1775],"class_list":["post-5324","post","type-post","status-publish","format-standard","hentry","category-java","tag-mongoclient","tag-mongocollection","tag-mongod","tag-mongodb","tag-mongoexception"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5324"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5324\/revisions"}],"predecessor-version":[{"id":5325,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5324\/revisions\/5325"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}