Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Aggregation framework. Group by sub-document fields
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
kirill kazachenko  
View profile  
 More options Nov 15 2012, 1:46 am
From: kirill kazachenko <cih.exe...@gmail.com>
Date: Wed, 14 Nov 2012 22:46:06 -0800 (PST)
Local: Thurs, Nov 15 2012 1:46 am
Subject: Re: Aggregation framework. Group by sub-document fields

WOW! Really thanks! It's that what i was need!

> db.sessions.aggregate(

{$project: {_id:1,events:1}},
{$unwind:"$events"},
{$group: {_id:"$events.name", count: {$sum:1}}})

made:

{
        "result" : [
                {
                        "_id" : "4en_homdfe",
                        "count" : 2
                },
                {
                        "_id" : "open_homdfe",
                        "count" : 2
                },
                {
                        "_id" : "open_home",
                        "count" : 2
                }
        ],
        "ok" : 1

}

среда, 14 ноября 2012 г., 20:12:45 UTC+3 пользователь Stephen Lee написал:

> Hi Kirill,

> Check out the $unwind operator
> <http://docs.mongodb.org/manual/reference/aggregation/#_S_unwind>which
> will take the documents in your aggregation pipeline and multiplexes the
> array entries as separate documents, eg.

> Before $unwind:

> {
> a: 1,
> b: [ 2,3,4]
> }

> After $unwind:

> { a:1,b:2 },
> { a:1,b:3 },
> { a:1,b:4 }

> After you include the $unwind in your pipeline, add the $group operation
> on "$events.name".

> -Stephen

> On Tuesday, November 13, 2012 4:13:35 PM UTC-5, kirill kazachenko wrote:

>> Hello, i have a question about grouping by sub elements.

>> i have next structure:
>> {
>> "name":"name",
>> "events": [
>>      {"name":"event1", "date": ...},
>>      {"name":"event2", "date": ...}
>>   ]
>> }

>> I'm need to extract and group all events by name with it's count, how can
>> i do this with the aggregation?

>> db.sessions.aggregate([{$group: {_id:"$events.name"}}])

>> i have response:

>> {
>>         "result" : [
>>                 {
>>                         "_id" : [ ]
>>                 },
>>                 {
>>                         "_id" : [
>>                                 "open_home",
>>                                 "open_home",
>>                                 "open_homdfe",
>>                                 "open_homdfe",
>>                                 "4en_homdfe",
>>                                 "4en_homdfe"
>>                         ]
>>                 }
>>         ],
>>         "ok" : 1
>> }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.