user2101699
user2101699

Reputation: 257

Mongo Spring @Document annotation

I'm trying to work with MongoTemplate and Spring and while looking on some other peoples code I was that everyone uses @Document annotation.

I did not used it at all up until not and everything worked fine for me. I'm afraid I'm missing something and could not find any specific detailed information about the benefits of @Document annotation.

Upvotes: 1

Views: 12195

Answers (2)

Adam Ostrožlík
Adam Ostrožlík

Reputation: 1416

This annotation serves only for specifying collection properties.

You can basically create simple class without any annotation needed.

Document annotation serves you for example when you are not happy with autogenerated collection name. If you do not specify @Document annotation, you still have the same result, for example

[
  {
    "_id": {"$oid": "62b43525de57ec7dec41a286"},
    "_class": "com.example.demo.Person",
    "name": "Adam"
  }
]

This annotation may however be important for some kind of annotation based process to target which document are in use (maybe for indexing and so on)

Same goes for Id annotation:

  • if you do not provide any id specification, Id field is automatically added by mongo
  • if you have field named as "id" of type BigInteger, ObjectId or String, then this field is automatically populated after insert

From the docs https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template.id-handling

11.5.1. How the _id Field is Handled in the Mapping Layer

MongoDB requires that you have an _id field for all documents. If you do not provide one, the driver assigns an ObjectId with a generated value. When you use the MappingMongoConverter, certain rules govern how properties from the Java class are mapped to this _id field:

A property or field annotated with @Id (org.springframework.data.annotation.Id) maps to the _id field.

A property or field without an annotation but named id maps to the _id field.

The following outlines what type conversion, if any, is done on the property mapped to the _id document field when using the MappingMongoConverter (the default for MongoTemplate).

If possible, an id property or field declared as a String in the Java class is converted to and stored as an ObjectId by using a Spring

Converter<String, ObjectId>. Valid conversion rules are delegated to the MongoDB Java driver. If it cannot be converted to an ObjectId, then the value is stored as a string in the database.

An id property or field declared as BigInteger in the Java class is converted to and stored as an ObjectId by using a Spring

Converter<BigInteger, ObjectId>.

If no field or property specified in the previous sets of rules is present in the Java class, an implicit _id file is generated by the driver but not mapped to a property or field of the Java class.

When querying and updating, MongoTemplate uses the converter that corresponds to the preceding rules for saving documents so that field names and types used in your queries can match what is in your domain classes.

Some environments require a customized approach to map Id values such as data stored in MongoDB that did not run through the Spring Data mapping layer. Documents can contain _id values that can be represented either as ObjectId or as String. Reading documents from the store back to the domain type works just fine. Querying for documents via their id can be cumbersome due to the implicit ObjectId conversion. Therefore documents cannot be retrieved that way. For those cases @MongoId provides more control over the actual id mapping attempts. Example 62. @MongoId mapping

public class PlainStringId {   @MongoId String id;  }

public class PlainObjectId {   @MongoId ObjectId id;  }

public class StringToObjectId {   @MongoId(FieldType.OBJECT_ID) String id;  }

The id is treated as String without further conversion. The id is treated as ObjectId. The id is treated as ObjectId if the given String is a valid ObjectId hex, otherwise as String. Corresponds to @Id usage.

Upvotes: 0

Elmar Brauch
Elmar Brauch

Reputation: 1366

@Document is an annotation provided by Spring data project.
It is used to identify a domain object, which is persisted to MongoDB.
So you can use it to map a Java class into a collection inside MongoDB.

If you don't use Spring Data, you don't need this annotation.

I wrote a German blog post about how to use Spring Data for MongoDB - @Document is used there also:
https://agile-coding.blogspot.com/2020/10/keine-ahnung-von-mongodb-dann-nimm.html

Upvotes: 1

Related Questions