📋 Geo Vector

by anon · 2026-06-22 17:20:58
← all clips

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.

Sample document shape

{

_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]

}

}

MongoDB Search index

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.

Query: vector search with distance-based geo filter

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" }

}

}

]);

Notes