Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-38835

Geospatial queries within $lookup pipeline

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.0.3
    • Component/s: Aggregation Framework, Geo
    • None

      I have a collection of areas:

      { "_id" : 1, name: "Area 1", "geometry" : { type: "Polygon", coordinates: <coordinates>} }
       { "_id" : 2, name: "Area 2", "geometry" : { type: "Polygon", coordinates: <coordinates>} }
       { "_id" : 3, name: "Area 3", "geometry" : { type: "Polygon", coordinates: <coordinates>} }

      I want to aggregate this so that every area (document) returned has a property containing all other areas within it's boundaries.
      The purpose is to be able to make holes in a polygon if other polygons are fully contained within it's boundaries.

      Result example where Area 2 and Area 3 are contained within the boundaries of Area 1.

      { "_id" : 1, name: "Area 1", "geometry" : { type: "Polygon", coordinates: <coordinates>}, holes: [{ "_id" : 2, name: "Area 2" }, { "_id" : 3, name: "Area 3" }] }
       { "_id" : 2, name: "Area 2", "geometry" : { type: "Polygon", coordinates: <coordinates>} }
       { "_id" : 3, name: "Area 3", "geometry" : { type: "Polygon", coordinates: <coordinates>} }

      I have tried using the `$lookup` aggregate but it seems like $geoWithin isn't supported inside the pipeline with my `let` variable as input.
      As i understand it I need to wrap `$geoWithin` part in a `$expr` to be able to use the `$$area_geometry` variable from the `let` statement but these operators doesn't seem compatible.

      So far my query ended up like this, but obviously this doesn't work.

      db.Areas.aggregate([
       {
         $lookup: 
         {
           from: 'Areas',
           let: { area_geometry: "$geometry" },
           pipeline: [
             { 
               $match: 
               { 
                 Geometry: { 
                   $geoWithin: { 
                     $geometry: $$area_geometry
                   } 
                 }
               }
             },
             { $project: { geometry: 0 } }
           ],
           as: 'holes'
         }
       }
      ])

            Assignee:
            daniel.hatcher@mongodb.com Danny Hatcher (Inactive)
            Reporter:
            kaht Kasper Hareskov Tygesen
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: