使用限制时,使用MongoDB获取总文档数 [英] Get a count of total documents with MongoDB when using limit
问题描述
我对优化与MongoDB合作的分页"解决方案感兴趣.我的问题很直接.我通常限制使用limit()
功能返回的文档数量.这迫使我发出不带limit()
函数的冗余查询,以便让我也捕获查询中的文档总数,因此我可以将其传递给客户端,让他们知道他们将不得不发出附加请求(s)检索其余文档.
I am interested in optimizing a "pagination" solution I'm working on with MongoDB. My problem is straight forward. I usually limit the number of documents returned using the limit()
functionality. This forces me to issue a redundant query without the limit()
function in order for me to also capture the total number of documents in the query so I can pass to that to the client letting them know they'll have to issue an additional request(s) to retrieve the rest of the documents.
是否可以将其压缩为1个查询?获取文档总数,但同时只能使用limit()
检索一个子集?与我正在解决的问题相比,有没有其他方法可以考虑这个问题?
Is there a way to condense this into 1 query? Get the total number of documents but at the same time only retrieve a subset using limit()
? Is there a different way to think about this problem than I am approaching it?
推荐答案
否,没有其他方法.两个查询-一个用于计数-一个具有限制.或者您必须使用其他数据库.例如,Apache Solr的运行就像您想要的那样.那里的每个查询都有有限的,并且返回totalCount.
No, there is no other way. Two queries - one for count - one with limit. Or you have to use a different database. Apache Solr for instance works like you want. Every query there is limited and returns totalCount.
这篇关于使用限制时,使用MongoDB获取总文档数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!