Skip to content

@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

Closed
maxim04 opened this issue Apr 18, 2022 · 4 comments
Closed

@MongoId does not affect Driver-assigned _id identifier #4026

maxim04 opened this issue Apr 18, 2022 · 4 comments
Assignees
Labels
type: bug A general bug

Comments

@maxim04
Copy link

maxim04 commented Apr 18, 2022

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 an ObjectId.
Here is the code:

@SpringBootApplication
@EnableMongoRepositories
public class Demo1Application implements CommandLineRunner {
    @Autowired
    UserRepository userRepository;

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        userRepository.deleteAll();

        userRepository.save(new User());
    }
}

@Document
class User {
    @MongoId
    String id;
}

interface UserRepository extends MongoRepository<User, String> {
}

And this is the data produced in the database:

{  "_id": {    "$oid": "625da3621aab8475434b3b66"  },  "_class": "com.example.demo.User"}

Even if I try @MongoId(targetType = FieldType.STRING) the _id field type in the database is still ObjectId. If I use spring's @Id annotation, the _id field gets written as an ObjectId which is no different to what @MongoId produces.
Is this a bug or am I misunderstanding the use of @MongoId? Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 18, 2022
@mp911de
Copy link
Member

mp911de commented Apr 21, 2022

Spring Data doesn't write any _id into the database (in your case) hence the driver itself generates the identifier value, see:

https://github.com/mongodb/mongo-java-driver/blob/master/bson/src/main/org/bson/codecs/BsonDocumentCodec.java#L149-L152

@MongoId has only an effect if you provide the identifier value. Also, there's no way to tell the document codec to use a string representation of the identifier as these things are outside of our control.

@maxim04
Copy link
Author

maxim04 commented Apr 21, 2022

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

@mp911de
Copy link
Member

mp911de commented Apr 22, 2022

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 String, BigDecimal, and byte[]. Let me take the ticket to our team to see whether we can do something about it.

@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Apr 22, 2022
@maxim04
Copy link
Author

maxim04 commented Apr 25, 2022

FWIW I noticed that when using @MongoId(targetType = FieldType.STRING) in entity "A" and having a DocumentReference to this entity in entity "B", the document reference field for entity "B" is written to the db as a String type, but entity "A" still gets an id of type ObjectId.

@christophstrobl christophstrobl self-assigned this May 18, 2022
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged for: team-attention An issue we need to discuss as a team to make progress labels May 19, 2022
@mp911de mp911de changed the title @MongoId has no effect @MongoId does not affect Driver-assigned _id identifier Sep 19, 2022
mp911de pushed a commit that referenced this issue Sep 19, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants