mongodb - 如何使用聚合查询在 mongodb 中获取计数?
问题描述
我试图使用聚合查询来获取数据库的计数。
我的情景
使用权
[{
"id": 1,
name: 'laptop'
}, {
"id": 2,
name: 'deasktop'
}]
询问
db.access.aggregate([
{
$match: {}
},
{
$group: {
total: { $sum: 1 }
}
}
])
执行查询
我得到了输出
[{
"total": 2
}]
异常输出
{
total: 1
result: [{
"id": 1,
name: 'laptop'
}, {
"id": 2,
name: 'deasktop'
}]
}
如何使用聚合查询获取此输出。
解决方案
推荐阅读
- c# - Parallel.ForEach 失败并显示“对象引用未设置为对象的实例”。例外
- regex - 如何在 dart/flutter 中检查一个字符串是否包含另一个子字符串
- c - 为什么我在运行此代码时会出现分段错误?
- r - 我想绘制测试的大小(例如 t test )与样本大小。如何在 R 中做到这一点?
- php - docker-compose nginx 向 php-fpm 返回 502
- c# - 验证 .NET Core Web API 控制器方法参数的更优惠方法是:返回 BadRequestResult 还是抛出异常?
- angular - 在Angular中对表格列进行排序
- php - 调用API时如何在PHP中打印出汉字?
- python - 如何从延迟的 dask 数组中获取特定的块?
- firebase - 安装 firebase 工具在 mac 上不起作用