【问题标题】:How can I retrieve a subcollection in firestore with spring framework?如何使用 spring 框架检索 firestore 中的子集合?
【发布时间】:2020-05-16 15:58:31
【问题描述】:

我有一个存储在 Firestore 上的数据库,其结构如下:

-Collection
----Document
-------Subcollection
----------Documents with Data
----Document
-------Subcollection
----------Documents with Data
----Document
-------Subcollection
----------Documents with Data

我想检索位于子集合中的文档,但我似乎无法使用以下代码:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collectionName = "states")
public class State {

    @DocumentId
    String name;

    int num_of_listings;
    Flux<Listing> listings;
}

Listing的代码是:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collectionName = "listings")
public class Listing {

    @DocumentId
    String id;

    String description;
}

想象每个“状态”都包含一些“列表”。每个 POJO 都附带一个控制器,该控制器具有 FirestoreReactiveRepository 类型的存储库注入,如下所示:

@Repository
public interface StateRepository extends FirestoreReactiveRepository<State> {
}
@RestController
@RequestMapping("/states")
@Slf4j
@RequiredArgsConstructor
public class StateController {

    private final StateRepository stateRepository;

    @GetMapping
    private Flux<State> getAllStates() {
        return stateRepository.findAll();
    }
}

当我使用getAllStates() 请求所有状态时,我得到一个空的listings 字段,而所有其他字段都按预期进行了初始化。我怎样才能实现所需的功能?

@Edit:如果有直接访问子集合的方法,也欢迎!

【问题讨论】:

    标签: java spring google-cloud-firestore spring-cloud


    【解决方案1】:

    看起来它不起作用,因为您在实体类中使用了 Flux 类型的字段。

    Flux<Listing> listings;
    

    但该项目目前不支持该类型的集合。尝试改用List&lt;Listing&gt;

    已编辑:

    您的实体类应如下所示:

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Document(collectionName = "states")
    public class State {
        
        @DocumentId
        String name;
        
        int num_of_listings;
    
        List<Listing> listings;
        
    }
    

    【讨论】:

    • 恐怕这不会导致任何变化(仍然为空)。你能详细说明project does not support collections of this type currently 吗?我不理解它,因为FirestoreReactiveRepositoryReactiveCrudRepository 的扩展,它在调用findAll() 方法时返回Flux&lt;T&gt; 类型的集合。
    • 澄清一下,该项目不支持 Flux 作为实体类中的集合类型。请参阅documentation 以查看支持的类型列表。
    • @Invalid_Path 请查看此sample app,特别是entity class。它演示了如何将集合用作 Firestore 实体中的字段。
    • 我将我的代码格式化为与您提供的完全一样,但当我从 Postman 发送请求时,我仍然收到返回的 Null 值。我发现了这个thread,我猜你参与了,虽然看起来,正如 Daniel Zou 所说,子集合可能不被支持。不幸的是,这个线程已有 2 年多的历史了,我找不到 Oskar Olsson 说你发布的问题 #2189,所以我无法验证它是否真的实现了(如果它实际上是一个问题)。
    • 澄清一下,我只在 List 字段中获得了 Null 值。其余字段正常检索。
    【解决方案2】:

    试试@DBRef(db="listings")

     @Data
        @AllArgsConstructor
        @NoArgsConstructor
        @Document(collectionName = "states")
        public class State {
    
            @DocumentId
            String name;
    
            int num_of_listings;
            @DBRef(db="listings")
            Flux<Listing> listings;
        }
    

    【讨论】:

    • 不幸的是,我的listings 字段上仍然有一个null 值。
    猜你喜欢
    • 2019-02-05
    • 2020-09-01
    • 2018-12-13
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2021-04-04
    • 2021-06-28
    • 2021-07-03
    相关资源
    最近更新 更多