I tried such a composite index in my application that also used spring data and worked correctly. You only need to adjust the index definition in the @CompoundIndex annotation:
@CompoundIndex(name = "aid_bid_idx", def = "{'id' : 1, 'b.id' : 1}") @Document(collection = "doc_a") public class A { @Field("id") private Integer id; @Field("b") private Collection<B> b; ... } public class B { @Field("id") private Integer id; ... }
If you run the query with an explanation (for example, as follows) in the mongo shell, you will see that the * aid_bid_idx * index will be used.
db.doc_a.find({ "id" : 1, "b.id" : 1}).explain()
The result will be something like this:
{ "cursor" : "BtreeCursor aid_bid_idx", ... }
source share