Group operations on arrays using the Mongo aggregation scheme

I am using mongodb 2.2. I would like to use the new aggregation structure to execute queries on my documents, but the elements are arrays.

Here is an example of my $ project project:

{ "type" : [ "ads-get-yyy", "ads-get-zzz" ], "count" : [ NumberLong(0), NumberLong(10) ], "latency" : [ 0.9790918827056885, 0.9790918827056885 ] } 

I want to group by type, so for "ads-get-yyy" you need to know how much the average is and how much is the average delay.

I would like to have something similar to the following query, but this works inside the elements of each array:

 db.test.aggregate( { $project : { "type" : 1, "count" : 1, "latency" : 1 } },{ $group : { _id: {type : "$type"}, count: {$avg: "$count"}, latency: {$avg: "$latency"} } }); 
+4
source share
1 answer

I'm just learning new autofocus, but I think you need the $unwind types first so you can group them. So something like:

 db.test.aggregate({ $project : { "type" : 1, "count" : 1, "latency" : 1 } },{ $unwind : "$type" },{ $group : { _id: {type : "$type"}, count: {$avg: "$count"}, latency: {$avg: "$latency"} } }); 
+1
source

Source: https://habr.com/ru/post/1435197/


All Articles