Annotating primitive and basic type fields
To store primitive and basic type fields in Mongo, you don't need any annotations:
...
private int myInt;
private Date myDate;
private List<String> myStrings;
private String[] stringArray;
...
By default, Morphia will try to map all the supported basic and primitive types to/from Mongo, including arrays of those types.
MongoDB only has the following data types:
- Integer (32-bit signed value)
- Long (64-bit signed value)
- Double (64-bit IEEE754 fp value)
- String
There are some conversions worth noting:
- float -> double
- byte -> int
- short -> int
- char -> String (l char)
Also, depending on what exists in the your model and the datastore we will try to convert things automagically. I mean, since there are only 3 number types in MongoDB (32/64-bit signed, 64-bit FP) it is pretty easy to up-convert. The following list defines what basic and primitive types can currently be persisted:
- enum (stored as String in Mongo)
- java.util.Date (stored as ms since epoch UTC)
- java.util.Locale (stored as String)
- com.mongodb.DBRef
- com.mongodb.ObjectId
As we showed in the example above, Morphia can also store a java.util.List, java.util.Set, and java.util.Map collections, and primitive arrays of any supported types.
If you want to exclude a field from being mapped to Mongo, use the @Transient annotation:
import com.google.code.morphia.annotations.Transient;
...
@Transient private int myTransientInt;
...
By default, Morphia uses the field name as the value name in Mongo. This can be overridden by using the @Property annotation, and specifying a name:
import com.google.code.morphia.annotations.Property;
...
@Property("my_integer")
private int myInt;
...