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

时间:2018-03-03 17:32:09

标签: spring mongodb spring-data spring-data-mongodb

我有以下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. 还有其他建议吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以使用以下方法完成此操作

BaseClassObject findByCustomer__id(String customerId)

您必须用下划线分隔参数,才能访问嵌套类的对象