-
Hi Team, We are facing an issue with mapping the _id field in our existing MongoDB collections. Our collections already exist, and several of them have a mix of String and ObjectId values for the _id field. In our previous setup with Spring, we handled this using: @Id
private String id;
This approach worked seamlessly.
However, when attempting the equivalent in Quarkus:
@BsonId
@BsonProperty("_id")
private String id; We encounter the following error: Failed to decode 'TestEntity'. Decoding 'id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID. Could you please suggest a workaround or best practice to handle this scenario in Quarkus? Thanks in advance for your guidance! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
/cc @FroMage (panache), @loicmathieu (mongodb,panache) |
Beta Was this translation helpful? Give feedback.
-
I talked to @nitinty offline about this first, but since I wasn't sure if this is intended behavior or not, I delegated to opening an issue. If someone wants a reproducer, here it is - https://github.com/xstefank/quarkus-reproducers/tree/main/mongo-id. |
Beta Was this translation helpful? Give feedback.
-
Hi, This should be the correct implementation in case you want a custom ID field that is a String: @BsonId
private String id; However, it seems you field is sometimes a String and sometimes an ObjectId, I'm afraid it cannot work OOTB as we use the PojoCode wich only do 1 <-> 1 mapping of BSON type to Java type. |
Beta Was this translation helpful? Give feedback.
-
@loicmathieu They have ObjectId in Mongo, and they want to pull ID as a String. So the question is if we should be doing ObjectId -> String mapping in the background. I personally also don't think that this is aligned with relational mapping in hibernate and it might be confusing if it differs for mongo. But this is your decision. |
Beta Was this translation helpful? Give feedback.
-
@loicmathieu @nitinty pinged me again about this today. So I gave it another try and I kind of got it working - https://github.com/xstefank/quarkus-reproducers/tree/main/mongo-id. So if @nitinty is OK with this solution, we can close this. |
Beta Was this translation helpful? Give feedback.
-
And here is the version with the custom codec if anyone ever runs into this issue again - https://github.com/xstefank/quarkus-reproducers/tree/main/mongo-id-reproducer. |
Beta Was this translation helpful? Give feedback.
And here is the version with the custom codec if anyone ever runs into this issue again - https://github.com/xstefank/quarkus-reproducers/tree/main/mongo-id-reproducer.