Spring Data query over two documents

2019-04-17 02:41发布

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!

0条回答
登录 后发表回答