Re: Referencing variable keys in aggregation framework

62 views
Skip to first unread message

William Zola

unread,
Oct 22, 2012, 11:50:12 AM10/22/12
to mongod...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages