首页 > 解决方案 > 按内部对象字段查询

问题描述

我有一个相当微不足道的问题,但我经常遇到错误。

所以我有一个Request包含列表字段的类calls。而且我想使用 Mongo 通过 -object 的to-field查询请求Call(例如,查找包含至少一个对 person 的调用的所有请求Alice)。

@Document
public class Request {
   @Id 
   String id;
   String content;
   List<Call> calls;

   //getters setters etc.
}

public class Call {
    String from;
    String to;
    DateTime timestamp;

    //getters setters etc.
}

在我的存储库中,我尝试执行不同类型的查询。但它们都返回错误。

interface RequestRepo extends ReactiveMongoRepository<Request, String> {

    @Query("select r from Request r where r.calls.to = :userId") 
    // I also tried "@Query({ 'calls.to' : ?0 }")
    Flux<Request> findAllByCallTo(@Param("userId") String userId);
}

但不幸的是我总是得到一个错误:

readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is STRING.

可能是什么问题以及编写查询的正确方法是什么?

标签: javamongodbspring-bootspring-mongodb

解决方案


推荐阅读