如何查询集合并限制返回的结果。假设我有一个包含 500M 文档的数据库,但我只想搜索并返回前 10 个匹配项,而不必搜索整个集合(出于性能原因)。
理想情况下,我可以在 O(m-n) 时间内返回第 n 到 m 个结果。
如果这可能或如何做到,有什么想法吗?
您可以通过申请来做到这一点skip https://docs.mongodb.com/v3.2/reference/method/cursor.skip/ and limit https://docs.mongodb.com/v3.2/reference/method/cursor.limit/:
db.collection.find(<query>).limit(<number>).skip(<number>)
您可以阅读有关性能问题的更多信息限制查询结果数量以减少网络需求 https://docs.mongodb.com/manual/tutorial/optimize-query-performance-with-indexes-and-projections/#limit-the-number-of-query-results-to-reduce-network-demand
Edit:
limit https://docs.mongodb.com/v3.2/reference/method/cursor.limit/ and skip https://docs.mongodb.com/v3.2/reference/method/cursor.skip/可以互换,skip https://docs.mongodb.com/v3.2/reference/method/cursor.skip/总是首先被调用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)