Spring data mongodb query for subdocument field

南楼画角 提交于 2019-12-23 10:55:15

问题


I'm using Spring Data's CrudRepository with mongodb and i have some issue to write a query which will select a document with specific subdocument value. Here's an example:

{
"_id" :,
"_class" :,
"matchHeader" : {
    "suspend" : {},
    "active" : true,
    "booked" : true,
    "eventId" : NumberLong(1009314492),
    "status" : ""
},
"matchInfo" : {

    }
}

}

i need to select the document with specific eventId field in matchHeader subdocument. i tried to write a function like this findByMatchHeaderEventId(id) but it doesn't helped at all.how can i achieve that?


回答1:


Property traversal for nested properties is explained in The Spring Data MongoDB Reference Documentation.

You need to properly define your domain object class (constructor/getters/setters omitted):

public class MyDocument {
  @Id
  private String id;
  private MatchHeader matchHeader;
  private MatchInfo matchInfo;
  ...
}

public class MatchHeader {
  private Map<,> suspend;
  private boolean active;
  private boolean booked;
  private Long eventId;
  private String status;
}

and your repository class

public interface MyDocumentController extends MongoRepository<MyDocument, String> {
  public List<MyDocument> findByMatchHeaderEventId(Long id);
}

Otherwise you can try the findByMatchHeader_EventId suggested in another answer.




回答2:


Try

findByMatchHeader_EventId

instead of

findByMatchHeaderEventId



来源:https://stackoverflow.com/questions/26887505/spring-data-mongodb-query-for-subdocument-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!