【发布时间】:2020-02-21 16:24:58
【问题描述】:
如果我使用discriminator key mongoose 设置模型,则会自动为所有查询添加鉴别键。
但如果我将某些键定义为index: true,则创建的索引不会以鉴别键为前缀。相反,它们是具有单个键的简单索引。
这不是最理想的吗?不是所有索引都应该是 discriminatorKey(例如__t)和我要索引的键的复合索引吗?
如果是,我应该而不是defining indexes at the path level(通过index: true)更喜欢这样设置它们:
Event.index({ __t: 1, type: 1 });
【问题讨论】:
-
不一定。
QueryPlanner分析您的查询以检查哪种索引组合更适合执行您的查询(检查自己:db.event.find({__t:"foo", type:"bar"}).explain())。因此添加__t不会改变您的查询性能。
标签: mongodb mongoose mongoose-schema mongodb-indexes