2014-06-23 74 views
1

我使用mongodb + springdata。我的文檔如下所示:設置Mongodb中嵌入文檔的索引

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

DocumentCopies是POJO在哪裏。是否可以使用Spring數據註釋在DocumentCopies字段之一上設置其他索引。

非常感謝!

回答

3

是的,你需要一個「點號」的形式引用您的其他POJO領域將被編入索引:

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

其中「childField」是你的「字段/屬性」,即名稱被索引。

+0

它不工作,它創建字段副本索引名稱copy.mychieldfieldname :( –

+0

@EK它應該在您的父對象映射集合中創建一個索引是「copy.childField」是該字段的實際名稱這應該是你想要的,你當然使用你所指的實際字段的名稱 –

+0

也許我錯了,但是這是它{ 「v」:1, 「key」:{ 「copy」: 1 }, 「NS」: 「Test.MyCollection」, 「名」: 「copy.url」, 「dropDups」:假的, 「疏」:。假 } –