Issue
Using Spring Data for Mongo (any recent version) I'm trying to set an index on a field that is persisted as a sub-document through the help of a org.springframework.core.convert.converter.Converter
(it is actually two fields)
My document class looks like this:
@Document
class Doc {
@Indexed
private MySuperField superField;
// id, getter setter ignored
}
While my document structure in database looks like this:
{
"superField": {
"field1": 123,
"field2": 456
}
}
The index gets created on superField
- while I would like it to be on superField.field1
. of course from Spring's point of view it is logical - only the Converter
has any notion of the sub document.
Is there any way to position the index on the right field while still using annotations? Otherwise I'll have to use a mongo template to explicitely create it...
Note : the actual use case behind this is to store a ZonedDateTime without loosing the original timezone information. First field is epoch, second is original timezone.
EDIT : I need to set expiration time of the index. Which makes compound indices irrelevant sadly.
Solution
I think compound indexes are the way to go here, try this:
@Document
@CompoundIndexes({
@CompoundIndex(name = "superfield_field1", def = "{ 'superField.field1': 1 }")
})
class Doc {
@Indexed
private MySuperField superField;
// id, getter setter ignored
}
Answered By - Charchit Kapoor
Answer Checked By - Willingham (JavaFixing Volunteer)