我的 mongodb 集合中有一些字段是搜索的可选部分。如果我不知道用户可能正在查询哪些字段,我如何才能一致地为该查询编制索引(即,无论参数如何,每个查询都将使用索引)?
最佳答案
您可以使用 Sparse Index
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field. The index is “sparse” because it does not include all documents of a collection. By contrast, non-sparse indexes contain all documents in a collection, storing null values for those documents that do not contain the indexed field.
db.addresses.createIndex( { "xmpp_id": 1 }, { sparse: true } )
关于Mongodb 索引可选字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28180066/