我需要使用 mongoDB Paging.count() 方法进行总计数,但添加条件时速度会变慢 有解决方法吗?
mongodb 分页需要总计数。 有没有一种快速方法可以在不使用 PagingModel.count() 方法的情况下获取它?
数据量超过100万条。
--add content
我们需要这个方法的原因是 count 包含一个条件。
exports.getTotalCount = async (shelterName = '', deviceId = '', eventType = '', period = '') => {
return new Promise((resolve, reject) => {
let where = {};
let collectTime = {};
if (period == 'entire' || period == '') {
collectTime['collectTime'] = {$lte: new Date(dayjs().format())}
} else if (period == 'today') {
collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "d").format()), $lte: new Date(dayjs().format())}
} else if (period == 'week') {
collectTime['collectTime'] = {$gte: new Date(dayjs().add(-7, "d").format()), $lte: new Date(dayjs().format())}
} else if (period == 'month') {
collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "M").format()), $lte: new Date(dayjs().format())}
} else {
collectTime['collectTime'] = {$gt: new Date(dayjs().add(-3, "M").format()), $lte: new Date(dayjs().format())}
}
where = {
'thingId': {$regex: shelterName},
'deviceId': {$regex: deviceId},
'eventState' : {$regex: eventType},
'collectTime': collectTime['collectTime']
};
EventModel
.find(where)
.count()
.lean()
.then(data => {
resolve(data);
}).catch(err => {
console.log(err);
})
});
}
每次添加一个条件,都需要很长时间。有什么办法可以解决这个问题吗?
A total count is required for mongodb paging.
Is there a quick way to get it without using the PagingModel.count() method?
The amount of data is over 1 million.
--add content
The reason we need this method is that count contains a condition.
exports.getTotalCount = async (shelterName = '', deviceId = '', eventType = '', period = '') => {
return new Promise((resolve, reject) => {
let where = {};
let collectTime = {};
if (period == 'entire' || period == '') {
collectTime['collectTime'] = {$lte: new Date(dayjs().format())}
} else if (period == 'today') {
collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "d").format()), $lte: new Date(dayjs().format())}
} else if (period == 'week') {
collectTime['collectTime'] = {$gte: new Date(dayjs().add(-7, "d").format()), $lte: new Date(dayjs().format())}
} else if (period == 'month') {
collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "M").format()), $lte: new Date(dayjs().format())}
} else {
collectTime['collectTime'] = {$gt: new Date(dayjs().add(-3, "M").format()), $lte: new Date(dayjs().format())}
}
where = {
'thingId': {$regex: shelterName},
'deviceId': {$regex: deviceId},
'eventState' : {$regex: eventType},
'collectTime': collectTime['collectTime']
};
EventModel
.find(where)
.count()
.lean()
.then(data => {
resolve(data);
}).catch(err => {
console.log(err);
})
});
}
Every time a condition is added, it takes a really long time. Is there any way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
collection.count() 是最快的,因为它读取集合元数据信息......
collection.count() is the quickest since it reads the collection metadata info ...
您可以有一个不同的集合,在其中存储集合的统计信息并保持更新(例如,将文档添加到“跟踪”集合,同时将“统计”集合中的计数添加 1)。
You can have a different collection in which you store the statistics for the collection and you keep it updated (e.g. you add a document to the "tracking" collection, you also add 1 to the count in the "statistics" collection).