I have two collections. “people” are connected with a “location” like so:
location_id = ObjectId()
db.people.insert(
{
_id : ObjectId(),
name : "Nick",
location : location_id
});
db.locations.insert(
{
_id : location_id,
city : "Cape Town"
});
I would like to create a histogram of locations giving the count of people in each city. But I can’t seem to do it with the Mongo group command because they are different collections. Is the correct way to do it with map/reduce?
map-reduce also won’t help you here. It also operates on a single collection.
It seems to me that you’re trying to apply your relational design skills here. Don’t do that. MongoDB requires a fresh approach.
If I were you, I’d probably denormalize location into people collection.
This way it’s possible to analyze this data with map-reduce, because everything is in the same collection.