mongodb中实现带总页数的嵌入式分页
我检查了与我的嵌入式分页情况相关的一个旧问题。虽然带有切片的嵌入式分页工作正常,但仍然缺少总页数解决方案,我想知道是否可以在服务器端获取评论的总大小。
有没有一种方法可以查询或计算服务器上嵌入数组的大小,而无需将整个文档获取到我的应用程序并手动计算?
我不介意为此进行 2 次查询,1 次用于对评论进行分页,1 次用于获取评论总数。如果我可以在一个查询中完成此操作,那就太棒了。
顺便说一下,我正在使用java驱动程序和spring-data mongodb。
请分享您的想法。谢谢 !
I've checked out an older question that is related with my embedded paging situation. Although the embedded paging with slicing works fine, the total pages solution is still missing, and i wonder whether it's possible to get the total size of the comments on the server side.
Is there a way to query or count the size of the embedded array on the server without having to fetch the whole document to my app and count it manually ?
I dont mind making 2 queries for this, 1 for paging the comment, 1 for getting the total of the comments. If i can do this in one query, that'll be amazing though.
By the way, i am using the java driver and spring-data mongodb.
Please share your thoughts. Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取嵌入文档总数的最佳/最快方法是创建一个附加字段并在每次更新/插入后重新计算计数。
文档将是这样的
然后你可以简单地在切片时包含
commentsCount
字段:实际上没有其他方法来计算嵌入文档的总数(我不是在谈论 m/r,因为它是实时请求很慢)
Best/fastest way to get total count of embedded documents is create an additional field and recalculate count after each update/insert.
Document will be like this
Then you can simply include
commentsCount
field when do slice:And actually there is no other way to calculate total count of embedded documents (i am not talking about m/r, because it is slow for real time requests)