Below is a MongoDB Search index and aggregation sample that runs vector search inside the $search stage and pre-filters results to documents within a radius of a central GeoJSON point.
Use this when you need geo filtering with vector search. The normal $vectorSearch aggregation stage only supports simple metadata filters; for Search operators like geoWithin, use the $search.vectorSearch operator.
{
_id: ObjectId("..."),
name: "Coffee Shop",
description: "Small cafe with espresso and pastries",
embedding: [0.0123, -0.0456, 0.0789 /* ... */],
location: {
type: "Point",
coordinates: [-73.9857, 40.7484] // [longitude, latitude]
}
}
Create a MongoDB Search index, not a vectorSearch index:
{
"mappings": {
"dynamic": false,
"fields": {
"embedding": {
"type": "vector",
"numDimensions": 1536,
"similarity": "cosine"
},
"location": {
"type": "geo"
},
"name": {
"type": "string"
}
}
}
}
Example index name:
places_vector_geo_idx
Adjust numDimensions to match your embedding model.
This searches for semantically similar documents, but only within 5000 meters of the central point.
const queryVector = [
/* embedding generated from the user's query text */
];
db.places.aggregate([
{
$search: {
index: "places_vector_geo_idx",
vectorSearch: {
path: "embedding",
queryVector: queryVector,
numCandidates: 200,
limit: 10,
filter: {
geoWithin: {
path: "location",
circle: {
center: {
type: "Point",
coordinates: [-73.9857, 40.7484] // [longitude, latitude]
},
radius: 5000 // meters
}
}
}
}
}
},
{
$project: {
_id: 1,
name: 1,
location: 1,
score: { $meta: "searchScore" }
}
}
]);