Spring Data MongoDB查询基于内部文档的id

时间:2017-08-08 12:27:28

标签: java mongodb spring-data

考虑以下entitis(EventReactions,EventLog):

public class EventReaction extends Entity {

    @Id
    private String id;

    private EventLog eventLog;
    ....    
    //getters and setters
}

public class EventLog extends Entity {

    @Id
    private String id;
    ....
    //getters and setters
}

我期待以下界面

public interface EventReactionRepository extends MongoRepository<T , String>{

    Optional<EventReaction> findByEventLog_Id(String id)

}

返回其eventLog id为id的eventReactions。但是,它已经存在于mogoDB中我的查询什么也没有返回!我的查询是否正确?

1 个答案:

答案 0 :(得分:0)

将您的代码更改为

Optional<EventReaction>

获取Optional<EventReaction> result= eventReactionRepository.findByEventLogId(new ObjectId("document-storage-id"));
Content-Type: application/json