Mongodb 索引可选字段

标签 mongodb indexing

我的 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/

相关文章:

mongodb - 使用 mongodb 和 redis 的缓存失效策略

node.js - 如何在终端重启mongodb?

c# - 用于嵌套查询的 RavenDB 索引

c++ - 如果 C++ 中已经存在,如何通过增加其索引来创建新文件夹

mongodb - 使用 mongod.conf 时,Mongo auth 无法登录

arrays - 更新 mongo 3.5 嵌套数组中的值

mysql - 使用 Neo4j 作为独立数据库的风险

MySQL:你能指定起始索引吗?

indexing - 如何在gnuplot中连接具有不同索引(一个数据文件)的点

mysql - 分片和索引