如何解决如何优化 mongodb 4.2 上的查找查询?
嗨,我有一个过滤器端点。当我按 createdAt 过滤时,响应时间太长。我看了一下图集,但没有任何索引建议。有人有什么建议吗?
Mongo 版本:4.2.12 猫鼬版本:5.7.1
我从前端发送的查询
{ "countryCode":"TR","createdAt":{
"startDate":"2020-02-01","endDate":"2021-03-01"
}
}
数据访问:
async filterBooks({
countryCode,bookIds,reasons,subReasons,authors,barcodes,statuses,userIds,createdAt,limit,offset,}) {
const { BookModel } = this;
const query = BookModel
.find({
...(countryCode ? { countryCode } : undefined),...(bookIds && Array.isArray(bookIds) && bookIds.length ? { _id: { $in: bookIds } } : undefined),...(reasons && Array.isArray(reasons) && reasons.length ? { reason: { $in: reasons } } : undefined),...(subReasons && Array.isArray(subReasons) && subReasons.length ? { subReason: { $in: subReasons } } : undefined),...(authors && Array.isArray(authors) && authors.length ? { author: { $in: authors } } : undefined),...(barcodes && Array.isArray(barcodes) && barcodes.length ? { barcode: { $in: barcodes } } : undefined),...(userIds && Array.isArray(userIds) && userIds.length ? { 'statusChangeLog.user': { $in: userIds } } : undefined),...(statuses && Array.isArray(statuses) && statuses.length ? { status: { $in: statuses } } : undefined),...(createdAt ? { createdAt: { $gte: new Date(createdAt.startDate),$lt: new Date(createdAt.endDate) } } : undefined),})
.populate('reason')
.populate('subReason');
const books = await query
.sort({ _id: -1 })
.limit(limit)
.skip(offset)
.lean()
.exec();
const total = await query.estimatedDocumentCount();
return {
books,total,};
}
当我查看新遗物统计数据时,我认为我的查询速度很慢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。