spring data mongodb查询嵌套对象和嵌套对象的性能

问题描述 投票:1回答:1

我有以下mongodb文档结构,我有几个问题:

[{
        "_id" : ObjectId("5a9ad6935625732968b720a6"),
        "customer" : {
                "_id" : ObjectId("5a9ab4b6acf09dde448e0348"),
                "email" : "...",
        },
        "pType" : {
                "_id" : ObjectId("5a9ab4b6acf09dde448e033a"),
                "name" : "..."
        },
        "dateTimeCreated" : ISODate("2018-03-03T17:08:35.351Z"),
        "_class" : "..."
}]

1.我目前要做的是选择具有特定客户ID的所有文档

我试过了

@Repository
interface CustomerPassRepo : ReactiveMongoRepository<CustomerPtype, String> {

    @Query(value = "{ 'customer.id' : ?0 }")
    fun findAllByCustomerId(id: String) : Flux<CustomerPtype>

    @Query(value = "{ 'customer._id' : ?0 }")
    fun findAllByCustomerId1(id: String) : Flux<CustomerPtype>

    fun findAllByCustomer_Id(id: String) : Flux<CustomerPtype>

    fun findAllByCustomer_id(id: String) : Flux<CustomerPtype>

}

没有用。

2.此类架构是否会影响查询性能?我的意思是第一种方法慢于

[{
        "_id" : ObjectId("5a9ad6935625732968b720a6"),
        "customerId" : {
                "_id" : "5a9ab4b6acf09dde448e0348",
                "email" : "...",
        },
        "pTypeId" : "5a9ab4b6acf09dde448e033a",
        "dateTimeCreated" : ISODate("2018-03-03T17:08:35.351Z"),
        "_class" : "..."
}]

第一种方法是占用更多空间,但它更好,因为我以后不需要加入数据。第一种方法是复制客户和pType。

3.还有其他建议吗?

spring mongodb spring-data spring-data-mongodb
1个回答
0
投票

我找到了一个解决方案,说我应该使用ObjectId作为方法参数。

以下所有工作:

@Query(value = "{ 'customer.id' : ?0 }")
fun findAllByCustomer_Id(objectId: ObjectId) : Flux<CustomerPass>

@Query(value = "{ 'customer.id' : ?0 }")
fun findAllByCustomer(objectId: ObjectId) : Flux<CustomerPass>

@Query(value = "{ 'customer._id' : ?0 }")
fun findAllByCustomer(objectId: ObjectId) : Flux<CustomerPass>

仍然不优雅,但它的工作。我还在寻找答案或替代解决方案。

资料来源:https://stackoverflow.com/a/34169761/869793

© www.soinside.com 2019 - 2024. All rights reserved.