[SERVER-38835] Geospatial queries within $lookup pipeline Created: 04/Jan/19  Updated: 12/May/21  Resolved: 07/Jan/19

Status: Closed
Project: Core Server
Component/s: Aggregation Framework, Geo
Affects Version/s: 4.0.3
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Kasper Hareskov Tygesen Assignee: Danny Hatcher (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

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'
   }
 }
])



 Comments   
Comment by Akshay Bilani [ 12/May/21 ]

Does Someone found this problem solution 

  1. SERVER-38835
Comment by Danny Hatcher (Inactive) [ 07/Jan/19 ]

Hello Kasper,

Thanks for your report. Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group.

Thank you,

Danny

Generated at Thu Feb 08 04:50:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.