Bug description I'm implementing vector similarity search using mongo atlas DB, using spring ai 1.0.0 stable version

vectorStore.similaritySearch(SearchRequest.builder().query(query).topK(5).build()); This code is failing with a class org.bson.types.ObjectId cannot be cast to class java.lang.String, when i tried to get the list of documents as described in the documentation this should be working without issues.

Adding the stack trace

java.lang.ClassCastException: class org.bson.types.ObjectId cannot be cast to class java.lang.String (org.bson.types.ObjectId is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap') at org.bson.Document.getString(Document.java:308) ~[bson-5.2.1.jar:na] at org.springframework.ai.vectorstore.mongodb.atlas.MongoDBAtlasVectorStore.mapMongoDocument(MongoDBAtlasVectorStore.java:243) ~[spring-ai-mongodb-atlas-store-1.0.0.jar:1.0.0]
at org.springframework.ai.vectorstore.mongodb.atlas.MongoDBAtlasVectorStore.lambda$doSimilaritySearch$1(MongoDBAtlasVectorStore.java:317) ~[spring-ai-mongodb-atlas-store-1.0.0.jar:1.0.0]

Potential solution could be to get the ID as an hexadecimal string value directly as there is no direct conversion from the mongo bson document.

String id = mongoDocument.getObjectId(ID_FIELD_NAME).toHexString();

Comment From: Kota-SH

Pull request available at - https://github.com/spring-projects/spring-ai/pull/3402

Comment From: ilayaperumalg

@pabloxtiyo We already have test cases covering the same use case and they pass in MongoDBAtlasVectorStoreIT. @Kota-SH I see your PR on the same. Thanks for the PR submission. But, the test seems to fail there.

Could you try and let us know how it goes.

Comment From: ilayaperumalg

Closing this as non-issue from the example posted. Please feel free to re-open if you still see the issue.

Comment From: lucasguardese

While using the MongoDBAtlasVectorStore class from the spring-ai module, we encountered a ClassCastException in the mapMongoDocument method when executing a similaritySearch query. The error occurs because the _id field is being accessed with mongoDocument.getString("_id"), but in our MongoDB collection, the _id is an ObjectId, which cannot be cast to String.

Since the mapMongoDocument method is internal and not customizable, and we rely on the default behavior of MongoDBAtlasVectorStore, this issue breaks similarity search execution in our application.

Would it be possible to update the internal implementation to safely convert the _id field using something like .toString() or ObjectId.toHexString() to support ObjectId-based documents?

Comment From: sleepo581

I've encountered the same issue.

Image

Unfortunately I don't have time to debug, but I believe this can be due to the following reasons:

When I use vectorStore#add(...), I set document id like this: doc.mutate().id(objectId.toString()).build()) And when documents are saved in vector store DB - ids are automatically converted to ObjectId type by Spring Data MongoDB.

Image

@pabloxtiyo @ilayaperumalg hope this helps a little bit.

@ilayaperumalg Perhaps code in this https://github.com/spring-projects/spring-ai/pull/3402 PR can be updated to check instance type and extract id accordingly?

Comment From: dsarlo-viso

Can we reopen this? Also running into this issue!

@ilayaperumalg