small medium large xlarge

Generic-user-small
16 Feb 2012, 05:46
Alex Varju (2 posts)

In the first example @db.phones.group()@ query, you include the string key @’components.area’@. This key doesn’t appear to do anything, as I can leave it off and get exactly the same results. The “mongodb doc examples”:http://www.mongodb.org/display/DOCS/Aggregation pass an object for this parameter, and following their example leads to different output:

db.phones.group({
  initial: {},
  reduce: function(phone, output, x) {
    if(!output[phone.components.area]) output[phone.components.area] = 0;
      output[phone.components.area]++;
  },
  cond: { 'components.number': { $gt : 5599999 } },
  key: { 'components.area': true }
})

[
	{
		"components.area" : 800,
		"800" : 50000
	},
	{
		"components.area" : 855,
		"855" : 50000
	}
]

Is there a reason the example includes a string-based key, or was this just a typo?

25233_377319146748_591736748_3799641_97127_n_pragsmall
22 Mar 2012, 20:06
Eric Redmond (21 posts)

Good point. That’s because of the structure of the I altered the reducer. I improved the example for the next version.

You must be logged in to comment