Mapping of _id fields:
MongoDB requires that all document have a _id field.
If we don't have an incoming _id field in use, it will create a objectid on its own.
{"_id": ObjectId ("53E0FF0B0364CB4A98CE3BFD"),"_class":"Org.springframework.data.mongodb.examples.hello.domain.Person","name":"John"," Age": the,"accounts": [ {"_id":NULL,"AccountNumber":"1234-59873-893-1","AccountType":"SAVINGS","Balance":123.45} ] }
Mongomappingconverter the Java class to the _id field according to the following rules:
1. @Id
org.springframework.data.annotation.Id
the field or attribute that is marked ().
2. There is no tag, but the name is the field or attribute of the ID (type is string or BigInteger).
Type mapping
If an object contains another object, it is stored by default in the _class field, for example
Public class Sample {contact value;} Public Abstract class Contact {...} Public class extends newnew"_class": "Com.acme.Sample", " Value ": {" _class ":" Com.acme.Person " }}
By using @typealias, you can change the value stored under _class to a fixed value, the following example is pers.
@TypeAlias ("pers")class person {}
Define your own mongotypemapper.
1. Using Java Config
class extends defaultmongotypemapper { //implement custom type mappinghere}
@ConfigurationclassSamplemongoconfigurationextendsabstractmongoconfiguration {@OverrideprotectedString GetDatabaseName () {return"Database"; } @Override PublicMongo Mongo ()throwsException {return NewMongo (); } @Bean @Override PublicMappingmongoconverter Mappingmongoconverter ()throwsException {mappingmongoconverter mmc=Super. Mappingmongoconverter (); Mmc.settypemapper (Customtypemapper ()); returnMMC; } @Bean Publicmongotypemapper Customtypemapper () {return NewCustommongotypemapper (); }}
2. Using XML
<type-mapper-ref= "Custommongotypemapper"/>< name= "Custommongotypemapper" class= "Com.bubu.mongo.CustomMongoTypeMapper" />