Mongo Distinct Aggegation(Mongo 不同的聚合)
问题描述
我正在尝试使用聚合框架在 mongo 中执行组计数,但结果并不完全符合预期.
I'm trying to use the aggregation framework to perform group counts in mongo but the results are not exactly as expected.
考虑下面的集合
$people->insert(array("user_id" => "1", "day" => "Monday", 'age' => 18));
$people->insert(array("user_id" => "3", "day" => "Monday", 'age' => 24));
$people->insert(array("user_id" => "1", "day" => "Monday", 'age' => 18));
$people->insert(array("user_id" => "1", "day" => "Monday", 'age' => 18));
$people->insert(array("user_id" => "2", "day" => "Monday", 'age' => 25));
$people->insert(array("user_id" => "4", "day" => "Monday", 'age' => 33));
$people->insert(array("user_id" => "1", "day" => "Tuesday", 'age' => 18));
$people->insert(array("user_id" => "2", "day" => "Tuesday", 'age' => 25));
$people->insert(array("user_id" => "1", "day" => "Wednesday", 'age' => 18));
$people->insert(array("user_id" => "2", "day" => "Thursday", 'age' => 25));
$people->insert(array("user_id" => "1", "day" => "Friday", 'age' => 18));
我使用下面的查询尝试计算一周中每一天的不同条目(user_id)的数量.
I use the query below try count the number of distinct entries (user_id) for each day of the week.
$query = array(
array(
'$project' => array(
'user_id' =>1,
'day' =>1,
),
),
array(
'$group' => array(
'_id' => array(
'user_id' => '$user_id',
'day' => '$day'),
'count' => array('$sum' => 1),
)
));
所以对于上面的集合,结果应该是
So for the collection above the results should be
Monday = 3 Tues = 2, Wed = 1, Thur = 1 and Friday = 1
但它不会将所有 DISTINCT users_id 的总数归为一天,而是每天为我提供每个现有 user_id 的总数.`
but it does not group the totals of all DISTINCT users_id under a day, and instead for each day it gives me a total for each existing user_id.`
结果(不完整)
[result] => Array
(
[0] => Array
(
[_id] => Array
(
[user_id] => 1
[day] => Friday
)
[count] => 1
)
[1] => Array
(
[_id] => Array
(
[user_id] => 1
[day] => Wednesday
)
[count] => 1
)
[2] => Array
(
[_id] => Array
(
[user_id] => 2
[day] => Tuesday
)
[count] => 1
)
... ... ...
有人可以帮我过滤每日总计,使其仅包括每天不同的总计
Can someone help me to filter the daily totals so that it only includes distinct totals per day
我查看了 $unwind 但不能真的让我头疼.`
I have looked at the $unwind but couldn't really get my head around it. `
推荐答案
如果我对问题的理解是正确的,那么您要解决的问题是
If I'm understanding the question right, what you're trying to get at is
totals of all DISTINCT users_id under a day
或者据我了解:每天唯一用户 ID 的计数.
Or as I understand it: Count of unique user_ids per day.
为此,您可以使用已有的组并删除计数,以便您只有一个唯一的 _id.user_id
和 _id.day
值:
For that, you could take the group you already have and cut out the count so that you just have a unique _id.user_id
and _id.day
value:
'$group' => array(
'_id' => array(
'user_id' => '$user_id',
'day' => '$day'
)
)
然后将其传送到另一个 $group
语句,该语句计算每天的文档数量,因为每个唯一的 user_id
/day代码>组合:
Then pipe that to another $group
statement that counts the number of documents per day, since there is exactly one for every unique user_id
/day
combination:
'$group' => array(
'_id' => '$_id.day',
'count' => array('$sum' => 1)
)
这篇关于Mongo 不同的聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mongo 不同的聚合
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01