Spring Data query over two documents

南楼画角 提交于 2019-12-06 12:43:36

问题


I have an m:n connection in my MongoDB, MessageUserConnection is the class between User and Message. Now I will get all MessageUserConnections where MessageUserConnection#confirmationNeeded is true, read is false and Message#receiveDate is not older than the last week. Is there any possibility to do this with Spring Data? Thanks a lot!

public class MessageUserConnection {

    @Id
    private String id;
    @DBRef
    private User userCreatedMessage;
    @DBRef
    private Message message;
    private Boolean confirmationNeeded;
    private Boolean read;
}

public class Message {

    @Id
    private String id;
    private String title;
    private String message;
    private DateTime receiveDate;
}

[EDIT] I have tried it by my own:

@Query("FROM MessageUserConnection AS muc WHERE muc.confirmationNeeded = ?0 AND muc.message.receiveDate = ?1")  
List<MessageUserConnection> findMessageUserConnectionByConfirmationNeededAndReceiveDate(final Boolean confirmationNeeded, final DateTime receiveDate);

and I get the following exception:

Caused by: com.mongodb.util.JSONParseException: FROM MessageUserConnection AS muc WHERE muc.confirmationNeeded = "_param_0" AND muc.message.receiveDate = "_param_1"

Does anyone know what I am doing wrong here? Thanks a lot!

[EDIT]

I run into another problem. My query currently looks like this.

 @Query("{$and : [{'confirmationNeeded' : ?0}, {'message.receiveDate' : ?1}]}")

where confirmationNeeded is a boolean and message.receiveDate is Joda#DateTime. With this query I get the following exception:

org.springframework.data.mapping.model.MappingException: Invalid path reference message.receiveDate! Associations can only be pointed to directly or via their id property!

Does that mean that I only can join to message.id? Thanks a lot!

来源:https://stackoverflow.com/questions/33478506/spring-data-query-over-two-documents

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