在 MongoDB 中限制结果但仍然得到完整计数? [英] Limiting results in MongoDB but still getting the full count?

查看:13
本文介绍了在 MongoDB 中限制结果但仍然得到完整计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了速度,我想将查询限制为 10 个结果

For speed, I'd like to limit a query to 10 results

db.collection.find( ... ).limit(10)

但是,我也想知道总数,所以说有 124 个,但我只有 10 个".有没有好的有效的方法来做到这一点?

However, I'd also like to know the total count, so to say "there were 124 but I only have 10". Is there a good efficient way to do this?

推荐答案

默认情况下,count() 忽略 limit() 并计算整个查询中的结果.因此,例如,当您执行此操作时,var a = db.collection.find(...).limit(10);运行 a.count() 将为您提供查询的总数.

By default, count() ignores limit() and counts the results in the entire query. So when you for example do this, var a = db.collection.find(...).limit(10); running a.count() will give you the total count of your query.

这篇关于在 MongoDB 中限制结果但仍然得到完整计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆