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 Referencing variable keys in aggregation framework

Received: by 10.58.170.6 with SMTP id ai6mr3610353vec.35.1350921023087;
        Mon, 22 Oct 2012 08:50:23 -0700 (PDT)
X-BeenThere: mongodb-user@googlegroups.com
Received: by 10.220.226.7 with SMTP id iu7ls4629930vcb.6.gmail; Mon, 22 Oct
 2012 08:50:13 -0700 (PDT)
Received: by 10.52.71.82 with SMTP id s18mr1425669vdu.9.1350921013072;
        Mon, 22 Oct 2012 08:50:13 -0700 (PDT)
Date: Mon, 22 Oct 2012 08:50:12 -0700 (PDT)
From: William Zola <william.z...@10gen.com>
To: mongodb-user@googlegroups.com
Message-Id: <213d820f-8cc7-4541-b545-3d8f0a4adf63@googlegroups.com>
In-Reply-To: <381bb734-ac12-4f41-af27-2a00c703140b@googlegroups.com>
References: <381bb734-ac12-4f41-af27-2a00c703140b@googlegroups.com>
Subject: Re: Referencing variable keys in aggregation framework
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_554_1168612.1350921012607"

------=_Part_554_1168612.1350921012607
Content-Type: multipart/alternative; 
	boundary="----=_Part_555_11531515.1350921012607"

------=_Part_555_11531515.1350921012607
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Hi Travis!

Unfortunately, the Aggregation framework does not provide a mechanism for 
aggregating fields where the name is not known at the time that the query 
is built.

As a workaround, you can run a map/reduce job that will collect all of the 
field names in advance, save them in a collection, and then build your 
aggregation query based on the contents of that collection. 

Let me know if you have further questions.  Have a great day!

 -William 

On Wednesday, October 17, 2012 6:19:25 PM UTC-4, Travis Dahlke wrote:
>
> I have documents set up where the keys have a variable integer suffix, e.g.
> {_id: foo, values_1: 2, values_2: 3, ... , values_n: 1, another_value_1: 
> 9, another_value_2: 8, ... , another_value_n: 7}
>
> I would like to use the aggregation framework to sum up all of the keys 
> starting with "value_" and all of the keys starting with "another_value_".
> Any idea how I could do this?
>
> I am already doing this with MongoDB's group command, but would like to 
> convert to the aggregation framework for lots of good reasons.
>
> Thanks
>

------=_Part_555_11531515.1350921012607
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hi Travis!<br><br>Unfortunately, the Aggregation framework does not provide=
 a mechanism for aggregating fields where the name is not known at the time=
 that the query is built.<br><br>As a workaround, you can run a map/reduce =
job that will collect all of the field names in advance, save them in a col=
lection, and then build your aggregation query based on the contents of tha=
t collection. <br><br>Let me know if you have further questions.&nbsp; Have=
 a great day!<br><br>&nbsp;-William <br><br>On Wednesday, October 17, 2012 =
6:19:25 PM UTC-4, Travis Dahlke wrote:<blockquote class=3D"gmail_quote" sty=
le=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left=
: 1ex;">I have documents set up where the keys have a variable integer suff=
ix, e.g.<div>{_id: foo, values_1: 2, values_2: 3, ... , values_n: 1, anothe=
r_value_1: 9, another_value_2: 8, ... , another_value_n: 7}</div><div><br><=
div>I would like to use the aggregation framework to sum up all of the keys=
 starting with "value_" and all of the keys starting with "another_value_".=
</div></div><div>Any idea how I could do this?</div><div><br></div><div>I a=
m already doing this with MongoDB's group command, but would like to conver=
t to the aggregation framework for lots of good reasons.</div><div><br></di=
v><div>Thanks</div></blockquote>
------=_Part_555_11531515.1350921012607--

------=_Part_554_1168612.1350921012607--