Skip to content

Commit 04191a7

Browse files
authored
Updated MongoDBAtlasVectorStore to use org.bson.Documents (#1026)
org.bson.Documents have been the preferred document structure since the 3.0 release of the mongo-java-driver
1 parent 8936aac commit 04191a7

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

vector-stores/spring-ai-mongodb-atlas-store/src/main/java/org/springframework/ai/vectorstore/MongoDBAtlasVectorStore.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.Map;
2222
import java.util.Optional;
2323

24-
import com.mongodb.BasicDBObject;
25-
2624
import com.mongodb.MongoCommandException;
2725
import org.springframework.ai.document.Document;
2826
import org.springframework.ai.embedding.EmbeddingModel;
@@ -140,16 +138,15 @@ private org.bson.Document createSearchIndexDefinition() {
140138
}
141139

142140
/**
143-
* Maps a BasicDBObject to a Spring AI Document
144-
* @param basicDBObject the basicDBObject to map to a spring ai document
145-
* @return the spring ai document
141+
* Maps a Bson Document to a Spring AI Document
142+
* @param mongoDocument the mongoDocument to map to a Spring AI Document
143+
* @return the Spring AI Document
146144
*/
147-
@SuppressWarnings("unchecked")
148-
private Document mapBasicDbObject(BasicDBObject basicDBObject) {
149-
String id = basicDBObject.getString(ID_FIELD_NAME);
150-
String content = basicDBObject.getString(CONTENT_FIELD_NAME);
151-
Map<String, Object> metadata = (Map<String, Object>) basicDBObject.get(METADATA_FIELD_NAME);
152-
List<Double> embedding = (List<Double>) basicDBObject.get(this.config.pathName);
145+
private Document mapMongoDocument(org.bson.Document mongoDocument) {
146+
String id = mongoDocument.getString(ID_FIELD_NAME);
147+
String content = mongoDocument.getString(CONTENT_FIELD_NAME);
148+
Map<String, Object> metadata = mongoDocument.get(METADATA_FIELD_NAME, org.bson.Document.class);
149+
List<Double> embedding = mongoDocument.getList(this.config.pathName, Double.class);
153150

154151
Document document = new Document(id, content, metadata);
155152
document.setEmbedding(embedding);
@@ -198,10 +195,10 @@ public List<Document> similaritySearch(SearchRequest request) {
198195
.build(),
199196
Aggregation.match(new Criteria(SCORE_FIELD_NAME).gte(request.getSimilarityThreshold())));
200197

201-
return this.mongoTemplate.aggregate(aggregation, this.config.collectionName, BasicDBObject.class)
198+
return this.mongoTemplate.aggregate(aggregation, this.config.collectionName, org.bson.Document.class)
202199
.getMappedResults()
203200
.stream()
204-
.map(this::mapBasicDbObject)
201+
.map(this::mapMongoDocument)
205202
.toList();
206203
}
207204

0 commit comments

Comments
 (0)