3

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 ?

3
  • Are you looking for @Indexed(unique = true, sparse = true)? Commented Aug 1, 2016 at 19:03
  • As you can see, Im not an expert at mongodb, but I think, you can use sparse=true, just in null case or when the field not exists. Maybe sparse is the best solution for my question - and you are right - but in other cases when you need another condition ( { age: { $gte: 21 } } sparse won't work. ( I think, my partial index example isn't the best ) Commented Aug 1, 2016 at 19:16
  • In case anybody interested, there is already a feature request in Spring jira: jira.spring.io/browse/DATAMONGO-1321 Commented Mar 31, 2019 at 4:57

1 Answer 1

7

I found your question while trying to do much the same thing.

As far as I can tell, neither spring-data-mongodb for spring-boot 1.5.x or 2.0.x supports Partial Indexes via the usual annotations.

However, spring-data-mongodb does allow you to create them programatically:

Index myIndex = new Index()
    .background()
    .unique()
    .named("my_index_name")
    .on("indexed_field_1", Sort.Direction.ASC)
    .on("indexed_field_2", Sort.Direction.DESC)
    .partial(PartialIndexFilter.of(
        Criteria.where("criteria_field_1")
        .is("BAR")));

DefaultIndexOperations indexOperations = new DefaultIndexOperations(mongoTemplate, "my_collection");

indexOperations.ensureIndex(myIndex);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeh, seems someone made a PR to add the annotation, but in the end they opted for the manual way. github.com/spring-projects/spring-data-mongodb/pull/380

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.