{"record":{"id":"c1346446b1dd5ef9","repo":"mongodb/laravel-mongodb","slug":"this-method-is-not-supported-by-mongodb-use-vectorsearch-c13464","errorCode":null,"errorMessage":"This method is not supported by MongoDB. Use vectorSearch() instead, which returns results ordered by vector score.","messagePattern":"This method is not supported by MongoDB\\. Use vectorSearch\\(\\) instead, which returns results ordered by vector score\\.","errorType":"exception","errorClass":"BadMethodCallException","httpStatus":null,"severity":"error","filePath":"src/Query/Builder.php","lineNumber":2064,"sourceCode":"     * Base method from Laravel >= 13.0\n     * ToDo: mark as `#[Override]` when we drop support for Laravel < 13.0\n     *\n     * @internal This method is not supported by MongoDB. Use vectorSearch() instead, which returns vectorSearchScore in the result.\n     */\n    public function selectVectorDistance($column, $vector, $as = null)\n    {\n        throw new BadMethodCallException('This method is not supported by MongoDB. Use vectorSearch() instead, which returns vectorSearchScore in the result.');\n    }\n\n    /**\n     * Base method from Laravel >= 13.0\n     * ToDo: mark as `#[Override]` when we drop support for Laravel < 13.0\n     *\n     * @internal This method is not supported by MongoDB. Use vectorSearch() instead, which returns results ordered by vector score.\n     */\n    public function orderByVectorDistance($column, $vector = [])\n    {\n        throw new BadMethodCallException('This method is not supported by MongoDB. Use vectorSearch() instead, which returns results ordered by vector score.');\n    }\n}\n","sourceCodeStart":2046,"sourceCodeEnd":2067,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Query/Builder.php#L2046-L2067","documentation":"orderByVectorDistance sorts SQL vector-search results by computed distance; MongoDB's Atlas Vector Search already returns results ordered by score, so the builder throws BadMethodCallException telling you to use vectorSearch(), which returns results sorted by vector score natively.","triggerScenarios":"Calling ->orderByVectorDistance($column, $vector) on a MongoDB query builder after performing a search.","commonSituations":"Codebase migrated from pgvector where explicit distance ordering was needed; new Laravel 13 vector APIs called on a MongoDB connection.","solutions":["Remove the orderBy call — vectorSearch() already returns results ordered by vector score","Use vectorSearch() instead of a manual search + orderByVectorDistance combination","Catch BadMethodCallException and drop the ordering step when using vectorSearch"],"exampleFix":"// before\n$docs = Doc::orderByVectorDistance('embedding', $vec)->limit(5)->get();\n// after\n$docs = Doc::vectorSearch('embedding', $vec)->limit(5)->get(); // already score-ordered","handlingStrategy":"fallback","validationCode":"if ($builder instanceof MongoDB\\Query\\Builder) { $results = $builder->vectorSearch($col, $vec)->limit($n)->get(); }","typeGuard":null,"tryCatchPattern":"try { $q->orderByVectorDistance($col, $vec); } catch (\\BadMethodCallException $e) { /* vectorSearch results are already score-ordered */ }","preventionTips":["Rely on vectorSearch()'s native score ordering; do not add distance orderBy","Remove explicit distance sorting when migrating from pgvector to MongoDB","Review the library's Atlas Vector Search guide before writing search queries"],"tags":["query-builder","vector-search","unsupported-method","mongodb"],"backgroundTag":"unsupported-operation","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}