Skip to content

Instantly share code, notes, and snippets.

@andineck
Last active January 12, 2016 06:49
Show Gist options
  • Save andineck/6a4994cbd92dc4464b55 to your computer and use it in GitHub Desktop.
Save andineck/6a4994cbd92dc4464b55 to your computer and use it in GitHub Desktop.
spring notes, mongodb

http://docs.spring.io/spring-data/mongodb/docs/1.5.4.RELEASE/reference/html/mapping-chapter.html

6.3.1 Mapping annotation overview

The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below

  • @Id - applied at the field level to mark the field used for identiy purpose.
  • @Document - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored. @Document(collection = "collectionName")
  • @DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.
  • @Indexed - applied at the field level to describe how to index the field.
  • @CompoundIndex - applied at the type level to declare Compound Indexes
  • @GeoSpatialIndexed - applied at the field level to describe how to geoindex the field.
  • @Transient - by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database
  • @PersistenceConstructor - marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject.
  • @Value - this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object. In order to reference a property of a given document one has to use expressions like: @Value("#root.myProperty") where root refers to the root of the given document.
  • @Field - applied at the field level and described the name of the field as it will be represented in the MongoDB BSON document thus allowing the name to be different than the fieldname of the class.

http://docs.spring.io/spring-data/data-mongo/docs/1.8.1.RELEASE/reference/html/#mapping-usage-annotations

The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below

@Id - applied at the field level to mark the field used for identiy purpose.

@Document - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored.

@DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.

@Indexed - applied at the field level to describe how to index the field.

@CompoundIndex - applied at the type level to declare Compound Indexes

@GeoSpatialIndexed - applied at the field level to describe how to geoindex the field.

@TextIndexed - applied at the field level to mark the field to be included in the text index.

@Language - applied at the field level to set the language override property for text index.

@Transient - by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database

@PersistenceConstructor - marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject.

@Value - this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key’s value retrieved in the database before it is used to construct a domain object. In order to reference a property of a given document one has to use expressions like: @Value("#root.myProperty") where root refers to the root of the given document.

@Field - applied at the field level and described the name of the field as it will be represented in the MongoDB BSON document thus allowing the name to be different than the fieldname of the class.

@Version - applied at field level is used for optimistic locking and checked for modification on save operations. The initial value is zero which is bumped automatically on every update.

@andineck
Copy link
Author

MongoDB Config in Spring-Boot:

application.yml

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: test
  profiles:
    active: test

https://bitbucket.org/mpatki01/spring-boot-todo/commits/77ff273a5376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment