2017-06-14 106 views
0

使用Spring Data MongoDB。而且我已經定義我的MongoRepository此查詢:如何將數組傳遞給MongoRepository @Query?

@Query("{'type':?0, 'tags.type':?1, 'tags.softSkill.name': {$all: ?2}}") 
List<Foo> findByTypeAndTags_TypeAndTags_SoftSkills(
     FooType type, TagType tagType, String[] softSkills); 

當我想用$all操作,我不能通過構建方法命名方式查詢,所以我使用@Query註解。

問題是,它不喜歡String[],我試圖通過?2參數。我嘗試了一個列表,但錯誤依然存在。

value expected to be of type java.util.List but is class java.lang.String toString:fooStr 

在一個蒙戈殼會是這樣:$all: ['foo1','foo2']

抽象的實體是這樣的:

@Data 
@Document(collection="foes") 
public abstract class Foo { 
    /*...*/ 
    protected List<Tag> tags = new ArrayList<>(); 
} 

標籤 asbtract POJO:

@Data 
public abstract class Tag { 

    protected final TagType type; 
    protected TagLevel level; 

} 

TagSoftSkill POJO:

@Data 
@EqualsAndHashCode(callSuper=true) 
public class TagSoftSkill extends Tag{ 

    private SoftSkill softSkill; //another pojo that has a "name" field 


    public TagSoftSkill() { 
     super(TagType.SOFT_SKILL); 
    } 


} 

我已經調試和softSkills打印[foo]。我修改的值是['foo'],但沒有變化。

+0

你能告訴我們輸入參數arg'softSkills'失敗的示例調用方法嗎?請將你的'Foo' bean添加到帖子中? – Veeram

+0

@Veeram ok,done – anat0lius

+1

我不能重新創建錯誤。在1.10.4 spring mongo發行版中,列表和數組都適用於我。你能爲異常添加堆棧跟蹤嗎? – Veeram

回答

0

我沒有提到我正在使用fongo。切換到真正的芒果和一切正常,因爲它應該。所以這是一個問題。