二十二、
MongoDB 复合查询
https://blog.csdn.net/weixin_39173093/article/details/80659407blog.csdn.net二十一、
无限滑动加载、分页加载
无限滑动加载和分页加载的利弊www.jianshu.com 网站信息量大,该采用分页式设计还是瀑布流滚动设计? - 优设网 - UISDCwww.uisdc.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 _id
s.
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." }
十九、
Mongodb高级查询Aggregation聚合组件分页
skip和limit
https://blog.csdn.net/qq_33556185/article/details/53099085blog.csdn.net十八、
官方文档:
cursor.skip() - MongoDB Manualdocs.mongodb.com十七、
两种分页方法的基准测试
Benchmark and compare the two approaches to paginate in MongoDBarpitbhayani.me十六、
实现分页的两种方法
Fast and Efficient Pagination in MongoDBarpitbhayani.me十五、
为什么MongoDB的cursor.skip()很慢?
简而言之,MongoDB必须迭代文档才能跳过它们。因此,当收集或结果集很大并且您需要跳过文档以进行分页时,调用cursor.skip
将是昂贵的。在浏览skip
我的源代码时发现它不使用任何索引,因此当结果集的大小增加时会变慢。
十四、
好问题!
“有多少是太多了?” - 当然,这取决于您的数据大小和性能要求。当我跳过超过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 Paging using Ranged Queries (Avoiding Skip())sammaye.wordpress.com十二、
我在Mongo的一个集合中有超过30万条记录。
当我运行这个非常简单的查询时:
db.myCollection.find().limit(5);
它只需几毫秒。
但是当我在查询中使用skip时:
db.myCollection.find().skip(200000).limit(5)
它不会返回任何内容......它会运行几分钟而不返回任何内容。
如何让它变得更好?
Slow pagination over tons of records in mongodbstackoverflow.com我发现将两个概念结合在一起是非常有效的(跳过+限制和查找+限制)。当您拥有大量文档(特别是较大的文档)时,跳过+限制的问题是性能不佳。find + limit的问题是你无法跳转到任意页面。我希望能够在不按顺序进行分页的情况下进行分页。
我采取的步骤是:
- 根据您对文档进行排序的方式创建索引,或者只使用默认的_id索引(这是我使用的)
- 了解起始值,页面大小和要跳转到的页面
- 项目+跳过+限制你应该开始的值
- 查找+限制页面的结果
如果我想得到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/restheartgithub.com十、
解决mongodb大数据量分页查询效率问题
https://blog.csdn.net/heihu_malice7/article/details/78958916blog.csdn.net九、
let b = await CaseInfos.count()
let b = await CaseInfos.countDocuments()
let c = await CaseInfos.estimatedDocumentCount()
https://blog.csdn.net/u010919083/article/details/94859638blog.csdn.net https://github.com/Automattic/mongoose/issues/6697github.com八、
https://groups.google.com/forum/#!topic/mongodb-user/PXMN1Qv_6hwgroups.google.com七、
查询以从集合中返回每个第N个值
Query to return every Nth value from the collectionjira.mongodb.org六、
MongoDB 随机查询获取一条或 N 条记录的方法
MongoDB 随机查询获取一条或 N 条记录的方法blog.chenxiaosheng.com五、
如何查看mongodb 某个collection 多少条数据
人类身份验证 - SegmentFaultsegmentfault.com四、
mongodb中我想在某一个表中查找第n行document
问一下,mongodb中我想在某一个表中查找第n行document,怎么做?_百度知道zhidao.baidu.com三、
人类身份验证 - SegmentFaultsegmentfault.com二、
https://blog.csdn.net/julywind1/article/details/82556425blog.csdn.net一、
mongoDB分页的两种方法 - laijiawei - 博客园www.cnblogs.com