$ unwind聚合框架中的对象

编程入门 行业动态 更新时间:2024-10-24 14:26:28
本文介绍了$ unwind聚合框架中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在MongoDB聚合框架中,我希望在对象(即JSON集合)上使用$ unwind运算符.看起来不是可能,是否有解决方法?有计划实施吗?

In the MongoDB aggregation framework, I was hoping to use the $unwind operator on an object (ie. a JSON collection). Doesn't look like this is possible, is there a workaround? Are there plans to implement this?

例如,从汇总文档中获取文章集合.假设还有一个附加字段"ratings",它是用户-> rating的映射.您可以计算每个用户的平均评分吗?

For example, take the article collection from the aggregation documentation . Suppose there is an additional field "ratings" that is a map from user -> rating. Could you calculate the average rating for each user?

除此之外,我对聚合框架感到非常满意.

Other than this, I'm quite pleased with the aggregation framework.

更新:这是每个请求的JSON集合的简化版本.我正在存储基因组数据.我无法真正将基因型设为数组,因为最常见的查找是获取随机人的基因型.

Update: here's a simplified version of my JSON collection per request. I'm storing genomic data. I can't really make genotypes an array, because the most common lookup is to get the genotype for a random person.

variants: [ { name: 'variant1', genotypes: { person1: 2, person2: 5, person3: 7, } }, { name: 'variant2', genotypes: { person1: 3, person2: 3, person3: 2, } } ]

推荐答案

无法使用聚合框架执行您要描述的计算类型-并且不,因为没有$unwind非数组方法.即使person:value对象是数组中的文档,$unwind也无济于事.

It is not possible to do the type of computation you are describing with the aggregation framework - and it's not because there is no $unwind method for non-arrays. Even if the person:value objects were documents in an array, $unwind would not help.

分组依据"功能(无论是在MongoDB中还是在任何关系数据库中)都是根据字段或列的值完成的.我们按字段的值分组,并根据另一个字段的值求和/平均值/等.

The "group by" functionality (whether in MongoDB or in any relational database) is done on the value of a field or column. We group by value of field and sum/average/etc based on the value of another field.

简单的示例是您所建议的一种变体,在示例文章集中添加了评级"字段,但不是从用户到评级的映射,而是这样的数组:

Simple example is a variant of what you suggest, ratings field added to the example article collection, but not as a map from user to rating but as an array like this:

{ title : title of article", ... ratings: [ { voter: "user1", score: 5 }, { voter: "user2", score: 8 }, { voter: "user3", score: 7 } ] }

现在,您可以使用以下方法进行汇总:

Now you can aggregate this with:

[ {$unwind: "$ratings"}, {$group : {_id : "$ratings.voter", averageScore: {$avg:"$ratings.score"} } } ]

但是按照您描述的方式构造的示例如下所示:

But this example structured as you describe it would look like this:

{ title : title of article", ... ratings: { user1: 5, user2: 8, user3: 7 } }

甚至这个:

{ title : title of article", ... ratings: [ { user1: 5 }, { user2: 8 }, { user3: 7 } ] }

即使您可以$unwind,这里也没有任何要汇总的内容.除非您知道所有可能的键(用户)的完整列表,否则您将无法做太多事情. [*]

Even if you could $unwind this, there is nothing to aggregate on here. Unless you know the complete list of all possible keys (users) you cannot do much with this. [*]

类似于您所拥有的关系数据库架构:

An analogous relational DB schema to what you have would be:

CREATE TABLE T ( user1: integer, user2: integer, user3: integer ... );

这不是要执行的操作,而是我们要执行以下操作:

That's not what would be done, instead we would do this:

CREATE TABLE T ( username: varchar(32), score: integer );

现在我们使用SQL进行聚合:

and now we aggregate using SQL:

select username, avg(score) from T group by username;

MongoDB有一个增强的要求,将来可能会允许您在聚合框架中执行此操作-可以将值投影为键,反之亦然.同时,总是有map/reduce.

There is an enhancement request for MongoDB that may allow you to do this in the aggregation framework in the future - the ability to project values to keys to vice versa. Meanwhile, there is always map/reduce.

[*]如果您知道所有唯一键,则有一种复杂的方法(您可以使用类似于此),但是如果您知道所有键,也可以对每个userX运行一系列db.articles.find({"ratings.user1":{$exists:true}},{_id:0,"ratings.user1":1})形式的查询,这将返回它们的所有评分,您可以简单地对其求和并求平均值,而不用进行聚合框架所需的非常复杂的预测.

[*] There is a complicated way to do this if you know all unique keys (you can find all unique keys with a method similar to this) but if you know all the keys you may as well just run a sequence of queries of the form db.articles.find({"ratings.user1":{$exists:true}},{_id:0,"ratings.user1":1}) for each userX which will return all their ratings and you can sum and average them simply enough rather than do a very complex projection the aggregation framework would require.

更多推荐

$ unwind聚合框架中的对象

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

发布评论

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

>www.elefans.com

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