-
Notifications
You must be signed in to change notification settings - Fork 1.1k
@MongoId
does not affect Driver-assigned _id
identifier
#4026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Spring Data doesn't write any
|
I see, this question arose as a result of repo lookups by DocumentReference id's returning empty results, for which I'll raise a separate issue. Thanks |
I wonder whether we could simply generate identifiers within Spring Data Mongo as the Id generation isn't tied to the server. Our mapping metadata has sufficient details and we could generate identifiers as |
FWIW I noticed that when using |
@MongoId
does not affect Driver-assigned _id
identifier
We now make sure to provide an id value that matches the desired target type when no id is set, and the property defines an explicit conversion target. Previously a new ObjectId would have been generated which leads to type inconsistencies when querying for _id. Closes #4026 Original pull request: #4057.
Using
spring-boot-starter-data-mongodb:2.6.6
spring-boot-starter:2.6.6
mongodb-driver-sync:4.4.2
spring-data-mongodb: 3.3.3
Documentation https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template.id-handling states that the
@MongoId
annotation can be used to control the data that gets written to mongodb. I want to write the auto generated_id
field to the database as a string, but no matter the annotations I use it always get's written as anObjectId
.Here is the code:
And this is the data produced in the database:
Even if I try
@MongoId(targetType = FieldType.STRING)
the_id
field type in the database is stillObjectId
. If I use spring's@Id
annotation, the_id
field gets written as anObjectId
which is no different to what@MongoId
produces.Is this a bug or am I misunderstanding the use of
@MongoId
? ThanksThe text was updated successfully, but these errors were encountered: