我有以下猫鼬模式:
var dataSchema = new Schema({
owner: { type: Schema.ObjectId, ref: 'User' },
time : { type: Date, default: Date.now },
eventCount:Number
});
对于某些 dataObjects,eventCount 已定义且为正数,对于其他 dataObjects,eventCount 未定义。我想设计一个索引,使这样的查询尽可能快:
db.datacollection.find({owner: <some ID>, eventCount: {$exists:true}, time: {<some time range>})
做这个的最好方式是什么?
这是我能想到的最佳解决方案,但我很想知道是否有人有更好的解决方案:
将 isEventCount 布尔变量添加到 dataSchema。设置 mongoose 中间件,以便在使用以下逻辑将对象保存到 db 之前计算 isEventCount。
if(eventCount > 0) {
isEventCount = true;
} else {
isEventCount = false;
}
然后建立这样的索引
db.datacollection.ensureIndex({user:1, isEventCount: 1, time:1})
并像这样运行我的查询
db.datacollection.find({owner: <some ID>, isEventCount: true, time: {<some time range>})
这种方法有几个缺点。即:
- 我正在数据库中保存冗余信息。
- 我必须编写额外的中间件代码来实现这一点。
- 我必须修改数据库中的现有条目。
有人知道更好的解决方案或可以帮助解决此问题的库吗?