Mongodb聚合计数数组/集大小

编程入门 行业动态 更新时间:2024-10-28 16:27:53
本文介绍了Mongodb聚合计数数组/集大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这里是我的问题:

模型:

< abc,date:Time.now,status:1user_id:[id1,id2, id4]}

{application:abc date:Date.yesterday,status:1,user_id:[ id1,id3,id5]}

{application:abc Time.yesterday-1,status:1,user_id:[ id1,id3,id5]}

需要计算一段时间内user_ids的唯一数。

预期结果:

{application:abc,status:1,unique_id_count:5}

聚合框架和计数mongodb外部的id。

{$ match:{application:abc}},{$ unwind:$ users},{$ group: {_id:{status:$ status}, users:{$ addToSet:$ users}}}

我的用户数组id非常大,所以我必须重复日期或者我将获得最大文档限制(16mb)。

我也可以$ group by

{year:{$ year:$ date},month :{$ month:$ date},day:{ $ dayOfMonth:$ date}

我也得到文档大小限制。

是否可以计算mongodb中的设置大小?

解决方案

以下内容将返回每个应用程序的uniqueUsers数。这将通过使用mongodb的管道特性将组操作应用于组操作的结果。

{$ match:{application :abc}, {$ unwind:$ users}, {$ group:{_id:$ status,users:{$ addToSet:$ users}}} , {$ unwind:$ users}, {$ group:{_id:$ _id,count:{$ sum:1}}}

干杯

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] }

I need to count the unique number of user_ids in a period of time.

Expected result:

{ application: "abc", status: "1", unique_id_count: 5 }

I'm currently using the aggregation framework and counting the ids outside mongodb.

{ $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).

I could also $group by

{ year: { $year: "$date" }, month: { $month: "$date" }, day: { $dayOfMonth: "$date" }

but I also get the document size limitation.

Is it possible to count the set size in mongodb?

thanks

解决方案

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.

{ $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"}}} jira.mongodb/browse/SERVER-4899

Cheers

更多推荐

Mongodb聚合计数数组/集大小

本文发布于:2023-11-22 17:18:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   集大   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!