Mongodb 聚合计数数组/集合大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14568283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongodb Aggregation count array/set size
提问by user2019059
Here's my problem:
这是我的问题:
Model:
模型:
{ application: "abc", date: Time.now, status: "1" user_id: [ id1, id2, id4] }
{ application: "abc", date: Time.yesterday, status: "1", user_id: [ id1, id3, id5] }
{ application: "abc", date: Time.yesterday-1, status: "1", user_id: [ id1, id3, id5] }
{ 应用程序:“abc”,日期:Time.now,状态:“1” user_id:[ id1, id2, id4] }
{ 申请:“abc”,日期:Time.yesterday,状态:“1”,user_id:[id1, id3, id5] }
{ 应用程序:“abc”,日期:Time.yesterday-1,状态:“1”,用户 ID:[ id1, id3, id5] }
I need to count the unique number of user_ids in a period of time.
我需要计算一段时间内 user_ids 的唯一数量。
Expected result:
预期结果:
{ application: "abc", status: "1", unique_id_count: 5 }
{申请:“abc”,状态:“1”,unique_id_count:5}
I'm currently using the aggregation framework and counting the ids outside mongodb.
我目前正在使用聚合框架并计算 mongodb 之外的 id。
{ $match: { application: "abc" } }, { $unwind: "$users" }, { $group: { _id: { status: "$status"}, users: { $addToSet: "$users" } } }
{ $match: { application: "abc" } }, { $unwind: "$users" }, { $group: { _id: { status: "$status"}, users: { $addToSet: "$users" } } }
My arrays of users ids are very large, so I have to iterate the dates or I'll get the maximum document limit (16mb).
我的用户 ID 数组非常大,所以我必须迭代日期,否则我将获得最大文档限制 (16mb)。
I could also $group by
我也可以 $group by
{ year: { $year: "$date" }, month: { $month: "$date" }, day: { $dayOfMonth: "$date" }
{ 年:{ $year:“$date”},月:{ $month:“$date”},日:{ $dayOfMonth:“$date”}
but I also get the document size limitation.
但我也得到了文档大小限制。
Is it possible to count the set size in mongodb?
是否可以计算 mongodb 中的设置大小?
thanks
谢谢
回答by cubbuk
The following will return number of uniqueUsers per application. This will apply an group operation to a result of a group operation by using pipeline feature of mongodb.
以下将返回每个应用程序的唯一用户数。这将使用 mongodb 的管道功能将组操作应用于组操作的结果。
{ $match: { application: "abc" } },
{ $unwind: "$users" },
{ $group: { _id: "$status", users: { $addToSet: "$users" } } },
{ $unwind:"$users" },
{ $group : {_id : "$_id", count : {$sum : 1} } }
Hopefully this will be done in an easier way in the following releases of mongo by a command which gives the size of an array under a projection. {$project: {id: "$_id", count: {$size: "$uniqueUsers"}}}
https://jira.mongodb.org/browse/SERVER-4899
希望这将在以下 mongo 版本中通过一个命令以更简单的方式完成,该命令给出投影下数组的大小。{$project: {id: "$_id", count: {$size: "$uniqueUsers"}}}
https://jira.mongodb.org/browse/SERVER-4899
Cheers
干杯
回答by mjhm
Sorry I'm a little late to the party. Simply grouping on the 'user_id' and counting the result with a trivial group works just fine and doesn't run into doc size limits.
对不起,我参加聚会有点晚了。简单地对“user_id”进行分组并用一个简单的组计算结果就可以了,并且不会遇到文档大小限制。
[
{$match: {application: 'abc', date: {$gte: startDate, $lte: endDate}}},
{$unwind: '$user_id'},
{$group: {_id: '$user_id'}},
{$group: {_id: 'singleton', count: {$sum: 1}}}
];
回答by xiansweety
Use $size to get the size of set.
使用 $size 获取集合的大小。
[
{
$match: {"application": "abc"}
},
{
$unwind: "$user_id"
},
{
$group: {
"_id": "$status",
"application": "$application",
"unique_user_id": {$addToSet: "$user_id"}
}
},
{
$project:{
"_id": "$_id",
"application": "$application",
"count": {$size: "$unique_user_id"}
}
}
]