问题
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