4

I'm using mongodb 2.2. I would like to use the new Aggregation Framework to do queries over my documents, but the elements are arrays.

Here an example of my $project result:

{ 
  "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" to know how much is the average of count and how much is the average of the latency.

I would like to have something similar to the next query, but that works inside of the elements of every array:

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

1 Answer 1

1

I'm just learning the new AF too, but I think you need to first $unwind the types so that you can group by 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"}
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.