{"record":{"id":"649ccdc7671b5130","repo":"spring-projects/spring-ai","slug":"failed-to-delete-documents-by-filter-649ccd","errorCode":null,"errorMessage":"Failed to delete documents by filter","messagePattern":"Failed to delete documents by filter","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"vector-stores/spring-ai-mongodb-atlas-store/src/main/java/org/springframework/ai/vectorstore/mongodb/atlas/MongoDBAtlasVectorStore.java","lineNumber":293,"sourceCode":"\t\tQuery query = new Query(org.springframework.data.mongodb.core.query.Criteria.where(ID_FIELD_NAME).in(idList));\n\t\tthis.mongoTemplate.remove(query, this.collectionName);\n\t}\n\n\t@Override\n\tprotected void doDelete(Filter.Expression filterExpression) {\n\t\tAssert.notNull(filterExpression, \"Filter expression must not be null\");\n\n\t\ttry {\n\t\t\tString nativeFilterExpression = this.filterExpressionConverter.convertExpression(filterExpression);\n\t\t\tBasicQuery query = new BasicQuery(nativeFilterExpression);\n\t\t\tDeleteResult deleteResult = this.mongoTemplate.remove(query, this.collectionName);\n\n\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\tlogger.debug(\"Deleted \" + deleteResult.getDeletedCount() + \" documents matching filter expression\");\n\t\t\t}\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new IllegalStateException(\"Failed to delete documents by filter\", e);\n\t\t}\n\t}\n\n\t@Override\n\tpublic List<Document> similaritySearch(String query) {\n\t\treturn similaritySearch(SearchRequest.builder().query(query).build());\n\t}\n\n\t@Override\n\tpublic List<Document> doSimilaritySearch(SearchRequest request) {\n\n\t\tString nativeFilterExpressions = (request.getFilterExpression() != null)\n\t\t\t\t? this.filterExpressionConverter.convertExpression(request.getFilterExpression()) : \"\";\n\n\t\tfloat[] queryEmbedding = this.embeddingModel.embed(request.getQuery());\n\t\tvar vectorSearch = new VectorSearchAggregation(EmbeddingUtils.toList(queryEmbedding), this.pathName,\n\t\t\t\tthis.numCandidates, this.vectorIndexName, request.getTopK(), nativeFilterExpressions);\n","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/vector-stores/spring-ai-mongodb-atlas-store/src/main/java/org/springframework/ai/vectorstore/mongodb/atlas/MongoDBAtlasVectorStore.java#L275-L311","documentation":"MongoDBAtlasVectorStore.doDelete wraps any exception raised while deleting documents matching a filter expression into an IllegalStateException(\"Failed to delete documents by filter\"). The MongoDB deleteMany call or the filter-expression conversion failed, so the deletion was not performed.","triggerScenarios":"Calling vectorStore.delete(String filterExpression) or delete(Filter...) on MongoDBAtlasVectorStore when the Mongo deleteMany fails (invalid filter document, connection loss, unacknowledged write, collection dropped) or the Atlas converter throws on an unsupported operator.","commonSituations":"Malformed or unsupported filter expression (e.g. LIKE op producing an invalid Atlas filter doc); MongoDB connection pool exhausted; database/collection permissions missing delete rights; transient network failure.","solutions":["Inspect the cause exception to see whether it is a converter error or a MongoDB driver error.","Simplify the filter to supported operators (EQ, IN, GT, etc.) — unsupported ops throw during conversion.","Verify MongoDB connectivity and that the configured database/collection exists.","Check the MongoDB user has delete permissions on the collection.","Retry the delete after transient connection issues."],"exampleFix":"// before\nvectorStore.delete(\"content LIKE 'foo%'\");\n// after\nvectorStore.delete(\"category == 'foo'\"); // supported operator","handlingStrategy":"try-catch","validationCode":"// ensure connectivity and supported filter ops before deleting\nmongoClient.getDatabase(databaseName).runCommand(new Document(\"ping\", 1)); // connectivity check\n// and validate filter ops are EQ/NE/GT/GTE/LT/LTE/IN/NIN as in error 568","typeGuard":null,"tryCatchPattern":"try {\n    vectorStore.delete(\"category == 'foo'\");\n} catch (IllegalStateException e) {\n    logger.error(\"Atlas delete failed: {}\", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());\n    // distinguish converter errors (unsupported op) from MongoDB driver errors\n}","preventionTips":["Use only supported filter operators to avoid converter failures.","Verify MongoDB user has delete permissions on the target collection.","Check connection pool settings to avoid exhaustion under load.","Test delete filters against a staging collection first."],"tags":["mongodb-atlas","vector-store","delete","filter-expression"],"backgroundTag":"database-write-failed","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}