How to use spring data of mongo @CompoundIndex with signatures?

Suppose I have objects such as:

@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; ... } 

Is it possible to use a compound expression with respect to A.id AND B.id together?

I mean, maybe, like:

 @CompoundIndex(name = "aid_bid_idx", def = "{'id', 'b.id'}") 

Thanks in advance.

+6
source share
2 answers

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", ... } 
+9
source

I had the same problem, for me the Miguel solution worked, but I had to wrap @CompoundIndex inside @CompoundIndexes, otherwise it did not work (I use Spring Roo).

 @CompoundIndexes({ @CompoundIndex(name = "aid_bid_idx", def = "{'id' : 1, 'b.id' : 1}") }) @Document(collection = "doc_a") public class A {...} 
+1
source

All Articles