如何获得文件数量并有效过滤? (猫鼬) [英] How can I get count of the Documents and filter them in efficient way? (mongoose)
问题描述
我正在实现仅在mongoDB中查找文档的搜索功能.我想在结果上使用.skip(x)
和.limit(x)
来模拟分页结果,但是我可以获取文档总数(在跳过和限制之前)并立即获得过滤结果吗?
I'm implementing search function that is simply find document in mongoDB. I want to .skip(x)
and .limit(x)
on result to simulate paging result, but can I get total count of document (before skip and limit) and get filtered result at once?
产生预期输出的代码:
db.Datas.find({ type: "Unknown" })
.then((result) => {
let count = result.length;
db.Datas.find({ type: "Unknown" })
.sort({ createdAt: -1 })
.skip((req.query.page - 1) * 10)
.limit(10)
.then((res) => {
res.json({ count: count, result: res });
});
})
.catch((err) => {});
但是以某种烦人的方式查询两次,在大型数据库中可能会很慢.
我尝试了类似find({}).then(x => { ... }).sort(...) ...
之类的方法,但由于它仅返回Promise而无法正常工作.
But querying twice it somehow annoying, and it might be slow at large database.
I tried something like find({}).then(x => { ... }).sort(...) ...
but isn't working because it only returns Promise.
我该如何高效地执行此操作? 还是只是获取整个文档并跳过,使用JS-way限制(使用.splice等)会更快更有效?
How can I do this things in efficient way? or is just getting whole documents and skip, limit with JS-way (using .splice, or etc..) will be faster and efficient?
推荐答案
You can use $facet aggregation to achieve this.
db.Datas.aggregate([
{
$match: {
"type": "Unknown"
}
},
{
$sort: {
createdAt: -1
}
},
{
$facet: {
totalRecords: [
{
$count: "total"
}
],
data: [
{
$skip: 0
},
{
$limit: 5
}
]
}
}
])
假设您有以下文件:
db={
"Datas": [
{
"_id": "5e390fc33285e463a0799689",
"type": "Known",
"createdAt": "2020-02-04T06:31:31.311Z",
"__v": 0
},
{
"_id": "5e390fd03285e463a079968a",
"type": "Known",
"createdAt": "2020-02-04T06:31:44.190Z",
"__v": 0
},
{
"_id": "5e390fda3285e463a079968b",
"type": "Unknown",
"createdAt": "2020-02-04T06:31:54.248Z",
"__v": 0
},
{
"_id": "5e390fdf3285e463a079968c",
"type": "Unknown",
"createdAt": "2020-02-04T06:31:59.993Z",
"__v": 0
},
{
"_id": "5e390fec3285e463a079968d",
"type": "Unknown",
"createdAt": "2020-02-04T06:32:12.336Z",
"__v": 0
},
{
"_id": "5e390ffd3285e463a079968e",
"type": "Unknown",
"createdAt": "2020-02-04T06:32:29.670Z",
"__v": 0
},
{
"_id": "5e3910163285e463a079968f",
"type": "Unknown",
"createdAt": "2020-02-04T06:32:54.131Z",
"__v": 0
},
{
"_id": "5e3910213285e463a0799690",
"type": "Unknown",
"createdAt": "2020-02-04T06:33:05.166Z",
"__v": 0
}
]
}
响应如下:
[
{
"data": [
{
"__v": 0,
"_id": "5e3910213285e463a0799690",
"createdAt": "2020-02-04T06:33:05.166Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e3910163285e463a079968f",
"createdAt": "2020-02-04T06:32:54.131Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e390ffd3285e463a079968e",
"createdAt": "2020-02-04T06:32:29.670Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e390fec3285e463a079968d",
"createdAt": "2020-02-04T06:32:12.336Z",
"type": "Unknown"
},
{
"__v": 0,
"_id": "5e390fdf3285e463a079968c",
"createdAt": "2020-02-04T06:31:59.993Z",
"type": "Unknown"
}
],
"totalRecords": [
{
"total": 6
}
]
}
]
如您所见,我们获得了包含过滤,排序,跳过和有限数据的总记录.
As you see, we got the total records with the filtered, sorted, skipped and limited data.
这篇关于如何获得文件数量并有效过滤? (猫鼬)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!