3

I use mongodb + springdata. My document looks like:

@Entity
@Document(collection="MyCollection")
public final class InfoItemMongoDBDocument {

    @Id
    private ObjectId id;

    @Column
    private String name;

    @Column
    @Indexed
    private int isFixed = 0;


    @Column
    private List<DocumentCopies> copy;

Where is DocumentCopies is POJO. Is it possible to set additional index on one of DocumentCopies field using Spring data annotations.

Thanks a lot!

1 Answer 1

5

Yes, you need a "dot notation" form referencing the field in your other POJO that is to be indexed:

@Document(collection="MyCollection")
@CompoundIndexes({
    @CompoundIndex( name="copy.childField", def="{'copy.childField': 1}")
})

Where "childField" is the name of your "field/property" that is being indexed.

Sign up to request clarification or add additional context in comments.

4 Comments

It doesnt work, it creates index on field copy with name copy.mychieldfieldname :(
@EK. it should be creating an index in your parent objects mapped collection that is "copy.childField" being the actual name of that field. This should be what you want. You of course use the name of the actual field you are referring to
Maybe I am wrong but this is it { "v" : 1, "key" : { "copy" : 1 }, "ns" : "Test.MyCollection", "name" : "copy.url", "dropDups" : false, "sparse" : false }
@EK. You are right. I had to hack this through @CompoundIndex

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.