0

I have two collections, which both have Geospatial Indexes:

people

[
  {
    "name": "Steve",
    ...
    "geometry": {"type": "Point", "coordinates": [90, 90]}
  },
  {
    "name": "John",
    ...
    "geometry": {"type": "Point", "coordinates": [-90, -90]}
  }
]

zones

[
  {
    "zone": "Place_0001001",
    "geometry": 
               {
                "type": "Polygon", 
                "coordinates": [
                                [90.0001, 90.0001],
                                [90.0002,90.0000],
                                ...
                               ]
               },
    "avg_income": 9999,
    "avg_age": 50
]

For analysis purposes I need the Zone information on a Person level, which requires to lookup the nearest Zone to each Person and then retrieve the fields.

I haven't been able to assign the nearest Zone to a Person, I understand the basis of the code should be something like this, a geoNear with a limit of 1:

db.people.aggregate([
    {
        $geoNear: {
            near: { type: "Point", coordinates: [90,90]},
            spherical: true,
            distanceField: "distance"
        },
      },
      {
        $limit: 1,
      },
    ]);

But I'm unsure on how to both reference the Zone and add it to the People collection.

corcufos
  • 1
  • 2

0 Answers0