两个字段的Mongodb索引,其中一个字段使用$gt和$lt

两个字段的Mongodb索引,其中一个字段使用$gt和$lt,mongodb,mongoose,Mongodb,Mongoose,因此,我收集了400万份文件(正在统计) 我注意到我的查询,检查用户是否已经玩了一场比赛,在当天变得非常缓慢 目前我有: const currentDate = new Date(); const startDate = new Date(currentDate).setHours(0, 0, 0, 0); const endDate = new Date(currentDate).setHours(23, 59, 59, 999); // user is just a string const

因此,我收集了400万份文件(正在统计)

我注意到我的查询,检查用户是否已经玩了一场比赛,在当天变得非常缓慢

目前我有:

const currentDate = new Date();
const startDate = new Date(currentDate).setHours(0, 0, 0, 0);
const endDate = new Date(currentDate).setHours(23, 59, 59, 999);
// user is just a string
const didAlreadySubmit = await Match.find({ user: req.body.user, submittedAt: { $gte: startDate, $lt: endDate } }).exec();
我应该制作什么样的索引来加快查询速度,或者有其他优化技巧吗?(目前执行大约需要5-7秒)


另外,如何在当前生产数据库中实现这个新索引?

您可以使用startDate和EndDate创建一个compoundex。MongoDB将在这两个字段上创建一个新索引。仅“提交”。我正在计算到达路线的开始和结束日期,如果用户在当天已经提交了分数,我会在数据库中查找。您是否已经在提交的数据上有索引?如果没有,您可以创建它,然后比较响应时间。