MongoDB Geo Search on two collections

I'm trying to figure out a way to geo query two collections:

  1. Person {name, age, groupid}
  2. Groups (id, name, geoLocation[x,y])

I need to find all near groups with persons with age > 18

Any idea how to do it without having to query the groups for each found person?

You have no joins in MongoDB so you need to find another way around this.

The next plausable method I would think of would be to store an array of age ranges with the group record:

{
    _id: {},
    name: {},
    geoLocation: {},
    age_groups: {
        'gt18': 1
    },
}

And then I would just query on that age_groups.gt18 field and pull out all records. This does mean, of course, you will require something to keep this field upto date. There are a couple of methods:

  • MR (Map Reduce) This would actually entail outputting to a summary table first so I don't recommend this really.
  • Event based Pre-aggregation. When a user joins or leaves a group you get their age and update the aggregated field accodingly.

I would personally go for the event based pre-aggregated method of doing things