In MongoDB, how do I perform a $geoWithin query as part of an aggregation pipeline that utilizes $lookup?

隐身守侯 提交于 2020-12-12 01:59:10

问题


I'm trying to use an aggregation pipeline to determine whether a specific territory has specific territories that match $geoWithin. Each territory has a geojson geometry object that is indexed appropriately. I'm not sure I am using let properly in this pipeline or if there is a way to coerce MongoDB into recognizing the parameters as appropriate geojson. I get the following error with the pipeline below:

unknown GeoJSON type: { type: "$$type", coordinates: "$$coords" }

const findTerritoriesWithin = (_id: string) => [
  {
    '$match': {
      '_id': new ObjectId(_id)
    }
  }, {
    '$lookup': {
      'from': 'territories', 
      'let': {
        'type': '$geometry.type', 
        'coords': '$geometry.coordinates'
      }, 
      'pipeline': [
        {
          '$match': {
            'geometry': {
              '$geoWithin': {
                '$geometry': {
                  'type': '$$type', 
                  'coordinates': '$$coords'
                }
              }
            }
          }
        }
      ], 
      'as': 'matchingTerritories'
    }
  }
];
const collection = db.collection('territories');
const results = await collection.aggregate(findTerritoriesWithin('5ef2408033a243ced1f02d1e'))

I also tried

{
  from: 'territories',
  let: { 
    geometry: '$geometry'
  },
  pipeline: [
    { 
      $match: {
        'geometry': {
          $geoWithin: {
            $geometry: '$$geometry'
          }
        }
      }
    }
  ],
  as: 'matching_territories'
}

but that yields: unknown geo specifier: $geometry: "$$geometry"

MongoDB version: 4.2.2.

Thanks in advance.

来源:https://stackoverflow.com/questions/62624316/in-mongodb-how-do-i-perform-a-geowithin-query-as-part-of-an-aggregation-pipeli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!