I would like to create a very simple annotated java POJO and save it into mongodb. Basically, it is:
@Component("vehicle")
@Scope("prototype")
@Document(collection = "vehicle")
@CompoundIndexes({
@CompoundIndex(name = "plateNumber_idx", def = "{ 'plateNumber' : 1 }", unique = true),
@CompoundIndex(name = "vin_idx", def = "{ 'vin' : 1 }", unique = true),
@CompoundIndex(name = "motorNumber_idx", def = "{ 'motorNumber' : 1 }", unique = true)
})
public class Vehicle {
private String plateNumber;
private String vin;
private String motorNumber;
... getters, setters, equal, hash etc. ....
}
It is working properly, but in my case I need to add a partial index to motorNumber field. The reason is: not necessary fill this field in, therefore this field can be null. But the other hand, not allowed to be two or more similar motorNumber - except, when those are null. I can add partial index(s) to vehicle collection by hand, but it will be more elegant way to do it by annotations. For example, here is my partial index:
{"motorNumber" : {"$exists" : true}}
My question is: How can I add this option to @CompoundIndex ? Or there are any other options ?
@Indexed(unique = true, sparse = true)?