分页第一页用0还是1_mongodb分页查询、复合查询

二十二、

MongoDB 复合查询

https://blog.csdn.net/weixin_39173093/article/details/80659407​blog.csdn.net

二十一、

无限滑动加载、分页加载

无限滑动加载和分页加载的利弊​www.jianshu.com 网站信息量大,该采用分页式设计还是瀑布流滚动设计? - 优设网 - UISDC​www.uisdc.com
ea02452f4245e6520b9ff4fe597bd752.png
PMCAFF社区 - PMCAFF互联网产品社区 产品经理人气组织::专注于研究互联网产品​www.pmcaff.com

二十、

Get the latest record from mongodb collection

Yet another way of getting the last item from a MongoDB Collection (don't mind about the examples):

> db.collection.find().sort({'_id':-1}).limit(1)

Normal Projection

> db.Sports.find()
{ "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2017", "Body" : "Again, the Pats won the Super Bowl." }
{ "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." }
{ "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }

Sorted Projection ( _id: reverse order )

> db.Sports.find().sort({'_id':-1})
{ "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
{ "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." }
{ "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2018", "Body" : "Again, the Pats won the Super Bowl" }

sort({'_id':-1}), defines a projection in descending order of all documents, based on their _ids.

Sorted Projection ( _id: reverse order ): getting the latest (last) document from a collection.

> db.Sports.find().sort({'_id':-1}).limit(1)
{ "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
Get the latest record from mongodb collection​stackoverflow.com
fc4f6ca45eecb193eebfc51869968d03.png

十九、

Mongodb高级查询Aggregation聚合组件分页

skip和limit

https://blog.csdn.net/qq_33556185/article/details/53099085​blog.csdn.net

十八、

官方文档:

cursor.skip() - MongoDB Manual​docs.mongodb.com
7e30c3f2c958c9cfe8bfde4d02edce98.png

十七、

两种分页方法的基准测试

Benchmark and compare the two approaches to paginate in MongoDB​arpitbhayani.me
08bd6eb36ea1d2034b052a09a63d05cd.png

十六、

实现分页的两种方法

Fast and Efficient Pagination in MongoDB​arpitbhayani.me
3350ee09ff44e1eb04cce4b45f8f8a95.png

十五、

为什么MongoDB的cursor.skip()很慢?

简而言之,MongoDB必须迭代文档才能跳过它们因此当收集或结果集很大并且您需要跳过文档以进行分页时,调用cursor.skip将是昂贵的。在浏览skip我的源代码时发现它不使用任何索引,因此当结果集的大小增加时会变慢

Why MongoDB's cursor.skip() is slow? | Codementor​www.codementor.io
1e097f76fedce7c4ac97c9f7fc13895b.png

十四、

好问题!

“有多少是太多了?” - 当然,这取决于您的数据大小和性能要求当我跳过超过500-1000条记录时,我个人感到不舒服。

实际答案取决于您的要求这是现代网站所做的(或者至少是其中一些)。

首先,navbar看起来像这样

1 2 3 ... 457

他们从总记录数和页面大小中获取最终页码让我们跳转到第3页这将涉及从第一条记录中跳过一些内容当结果到达时您知道第3页的第一条记录的ID

1 2 3 4 5 ... 457

让我们再跳过一些,转到第5页

1 ... 3 4 5 6 7 ... 457

你明白了。在每个点上,您可以看到第一页最后一页和当前页以及当前页面前后两页

查询

var current_id; // id of first record on current page.

// go to page current+N
db.collection.find({_id: {$gte: current_id}}).
              skip(N * page_size).
              limit(page_size).
              sort({_id: 1});

// go to page current-N
// note that due to the nature of skipping back,
// this query will get you records in reverse order 
// (last records on the page being first in the resultset)
// You should reverse them in the app.
db.collection.find({_id: {$lt: current_id}}).
              skip((N-1)*page_size).
              limit(page_size).
              sort({_id: -1});
MongoDB ranged pagination​stackoverflow.com
fc4f6ca45eecb193eebfc51869968d03.png

十三、

MongoDB Paging using Ranged Queries (Avoiding Skip())​sammaye.wordpress.com
6fdf4dbf49357209062a0deb89904c15.png

十二、

我在Mongo的一个集合中有超过30万条记录。

当我运行这个非常简单的查询时:

db.myCollection.find().limit(5);

它只需几毫秒。

但是当我在查询中使用skip时:

db.myCollection.find().skip(200000).limit(5)

它不会返回任何内容......它会运行几分钟而不返回任何内容。

如何让它变得更好?

Slow pagination over tons of records in mongodb​stackoverflow.com
fc4f6ca45eecb193eebfc51869968d03.png

我发现将两个概念结合在一起是非常有效的(跳过+限制和查找+限制)。当您拥有大量文档(特别是较大的文档),跳过+限制的问题是性能不佳。find + limit的问题是你无法跳转到任意页面。我希望能够在不按顺序进行分页的情况下进行分页

我采取的步骤是:

  1. 根据您对文档进行排序的方式创建索引或者只使用默认的_id索引(这是我使用的)
  2. 了解起始值页面大小和要跳转到的页
  3. 项目+跳过+限制你应该开始的值
  4. 查找+限制页面的结果

如果我想得到16条记录的第5432页(在javascript中),它看起来大致相同:

let page = 5432;
let page_size = 16;
let skip_size = page * page_size;

let retval = await db.collection(...).find().sort({ "_id": 1 }).project({ "_id": 1 }).skip(skip_size).limit(1).toArray();
let start_id = retval[0].id;

retval = await db.collection(...).find({ 
"_id": { "$gte": new mongo.ObjectID(start_id) } }).sort({ "_id": 1 }).project(...).limit(page_size).toArray();

十一、

Mongodb Skip is slow

关于游标的讨论

结论是:从本质上讲,我们不再使用skip,而是基于纯范围的分页......

Poor performance paging through large result sets · Issue #1 · SoftInstigate/restheart​github.com
91e03689989a48a3c5650f60300f2566.png

十、

解决mongodb大数据量分页查询效率问题

https://blog.csdn.net/heihu_malice7/article/details/78958916​blog.csdn.net

九、

let b = await CaseInfos.count()

let b = await CaseInfos.countDocuments()

let c = await CaseInfos.estimatedDocumentCount()

https://blog.csdn.net/u010919083/article/details/94859638​blog.csdn.net https://github.com/Automattic/mongoose/issues/6697​github.com

八、

https://groups.google.com/forum/#!topic/mongodb-user/PXMN1Qv_6hw​groups.google.com

七、

查询以从集合中返回每个第N个值

Query to return every Nth value from the collection​jira.mongodb.org

六、

MongoDB 随机查询获取一条或 N 条记录的方法

MongoDB 随机查询获取一条或 N 条记录的方法​blog.chenxiaosheng.com
00ed54697a6926478a85bc9390248e1d.png

五、

如何查看mongodb 某个collection 多少条数据

人类身份验证 - SegmentFault​segmentfault.com

四、

mongodb中我想在某一个表中查找第n行document

问一下,mongodb中我想在某一个表中查找第n行document,怎么做?_百度知道​zhidao.baidu.com
4658687f5b249f748e6aeb0545685e24.png

三、

人类身份验证 - SegmentFault​segmentfault.com

二、

https://blog.csdn.net/julywind1/article/details/82556425​blog.csdn.net

一、

mongoDB分页的两种方法 - laijiawei - 博客园​www.cnblogs.com
814911ee45e012e259c6e3c6aa169758.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值