javascript - 如何在 Mongodb 的 find 中计算文档?
问题描述
我正在尝试计算 mongodb 请求中的文档。
我可以计算-s 回调,但总是count = docs.length
给出. A 可以做 2 个相同的请求,只是替换为,但似乎是错误的toArray()
10
find
count
let count;
db.get().collection('images').find({
$and: [
{tags: { $in: tags }},
{date: {$gte: date.toISOString()}},
{title:{$regex: query, $options: "$i"}},
]},
(err, docs)=>{docs.toArray((err, res)=>{
count= res.length
})}
)
.skip(0).limit(10).toArray((err, docs)=>{
res = {data:docs, dataLength:count }
cb(err, res);
// })
});
我得到这个错误:TypeError: Cannot read property 'skip' of undefined
解决方案
你可能总是收到 10 回,因为当你这样做时,你特别说“给我 10 回” .limit(10)
。现在,您收到该错误的原因是因为.skip
必须添加到.find()
. 这是您的查询应如下所示:
db.collection('images').find({
$and: [
{tags: { $in: tags }},
{date: {$gte: date.toISOString()}},
{title:{$regex: query, $options: "$i"}},
]
}).skip(0).limit(10).toArray((err, docs) => {
if (err) { console.log(err) }
else {
let res = {data:docs, dataLength:docs.length}
// do any other logic here
}
})
推荐阅读
- swift - 使用 IAP 订阅的正确方法
- c# - 通过在 C# 中使用 Moq 模拟外部 DLL 进行单元测试
- c# - 即使输入有效,错误提供程序也不会自行清除
- mysql - 重新加载页面时获取 JSON 对象
- node.js - 通过 Vue CLI 添加 Vuetify 会导致“找不到模块”错误
- python - 使用 Pandas 逐年获取值的总和
- node.js - React App 的 Heroku 部署问题 - 找不到模块
- excel - Excel VBA 根据名称过滤多个工作表并另存为单独的文件
- php - 为什么在 Lumen 上运行 php artisan 时会发生错误
- vba - 循环通过 ODBC 连接