如何在 MongoDB 中指定自然顺序? [英] How can I specify the natural ordering in MongoDB?
问题描述
有没有办法在 mongodb 中指定数据的自然顺序,类似于主索引如何对 RDBMS 表中的数据进行排序?
Is there a way to specify the natural ordering of data in mongodb, similar to how a primary index would order data in a RDBMS table?
我的用例是我的所有查询都返回按日期字段排序的数据,比如生日.根据MongoDB:排序和自然顺序,标准(非上限)集合大致是插入顺序,但不能保证.这意味着在检索数据后需要进行排序.
My use case is that all my queries return data sorted by a date field, say birthday. According to MongoDB: Sorting and Natural Order, the natural order for a standard (non-capped) collection is roughly the insertion order, but not guaranteed. This would imply sorting is needed after the data is retrieved.
推荐答案
我相信你所指的是一个 聚集索引,不是主索引.
I believe what you are referring to is a clustered index, not a primary index.
MongoDB 2.0 没有聚集索引功能,因此常规索引日期将是您检索的最有效选择.
MongoDB 2.0 does not have a clustered index feature, so a regular index on date would be your most efficient option to retrieve.
考虑使用 MongoDB 在磁盘上的物理顺序可能是过早的优化.MongoDB 使用内存映射文件,因此根据您的工作集 + 查询 + RAM,您可能不需要像预期的那样频繁地从磁盘加载数据.
It's probably premature optimization to think about the physical order on disk with MongoDB. MongoDB uses memory-mapped files, so depending on your working set + queries + RAM you may not need to load data from disk as often as expected.
这篇关于如何在 MongoDB 中指定自然顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!