使用 Spring 数据 MongoDB ReactiveMongoRepository 的切片投影?
Sliced projections with Spring Data MongoDB ReactiveMongoRepository?
我有一个名为 myCollection
的集合,其中包含以下格式的文档:
{
"_id" : "1",
"myArray" : [ { x: 1, y: "a" }, { x: 2, y: "b" }, { x: 3, y: "c" }, { x: 4, y: "d" }, { x: 5, y: "e" }]
}
我想做的是构造一个查询,returns myArray
中某些元素的 slice 作为投影。
即假设我的文档是这样定义的:
@Document(collection = "myCollection")
data class MyDocument(@Id val myId : String, val myArray : List<MyItem>)
其中 MyItem
定义如下:
data class MyItem(val x: Int, val y: String)
现在我想创建一个函数,该函数 returns 给定 MyDocument
的特定偏移量和项目计数(或 "page")的 MyItem
列表一个特定的 ID。
这是我试过的(使用 projections):
data class MyArrayProjection(val myArray: List<MyItem>)
interface MyRepository : ReactiveMongoRepository<MyDocument, String> {
fun findByMyId(myId: String, pageable: Pageable): Flux<MyArrayProjection>
}
使用例如
调用此函数时我希望看到的内容
myRepository.findByMyId("1", PageRequest.of(1, 2))
是 returns 包含 MyItem(x=3, y="c")
和 MyItem(x=4, y="d")
的 Flux
但它是空的。
生成的 MongoDB 查询如下所示:
{
"find" : "myCollection",
"filter" : {
"_id" : "1"
},
"projection" : {
"myArray" : 1
},
"skip" : 2,
"limit" : 2,
"batchSize" : 256
}
我怀疑发生的事情是 Pageable
实例在聚合 (MyDocument
) 上运行,而不是 "inside" myArray
字段,这就是我怀疑的原因我想以某种方式使用 $slice 运算符。
我怎样才能做到这一点?如果使用 ReactiveMongoRepository then I'm fine with using ReactiveMongoOperations.
无效
我设法通过更改解决了这个问题:
interface MyRepository : ReactiveMongoRepository<MyDocument, String> {
fun findByMyId(myId: String, pageable: Pageable): Flux<MyArrayProjection>
}
至:
interface MyRepository : ReactiveMongoRepository<MyDocument, String> {
@Query(value = "{ '_id' : ?0 }", fields = "{ 'myArray': { '$slice': ?1 } }")
fun findByMyId(myId: String, slice: Array<Int>): Flux<MyArrayProjection>
}
然后调用它:
myRepository.findByMyId("1", arrayOf(1, 2))
我有一个名为 myCollection
的集合,其中包含以下格式的文档:
{
"_id" : "1",
"myArray" : [ { x: 1, y: "a" }, { x: 2, y: "b" }, { x: 3, y: "c" }, { x: 4, y: "d" }, { x: 5, y: "e" }]
}
我想做的是构造一个查询,returns myArray
中某些元素的 slice 作为投影。
即假设我的文档是这样定义的:
@Document(collection = "myCollection")
data class MyDocument(@Id val myId : String, val myArray : List<MyItem>)
其中 MyItem
定义如下:
data class MyItem(val x: Int, val y: String)
现在我想创建一个函数,该函数 returns 给定 MyDocument
的特定偏移量和项目计数(或 "page")的 MyItem
列表一个特定的 ID。
这是我试过的(使用 projections):
data class MyArrayProjection(val myArray: List<MyItem>)
interface MyRepository : ReactiveMongoRepository<MyDocument, String> {
fun findByMyId(myId: String, pageable: Pageable): Flux<MyArrayProjection>
}
使用例如
调用此函数时我希望看到的内容myRepository.findByMyId("1", PageRequest.of(1, 2))
是 returns 包含 MyItem(x=3, y="c")
和 MyItem(x=4, y="d")
的 Flux
但它是空的。
生成的 MongoDB 查询如下所示:
{
"find" : "myCollection",
"filter" : {
"_id" : "1"
},
"projection" : {
"myArray" : 1
},
"skip" : 2,
"limit" : 2,
"batchSize" : 256
}
我怀疑发生的事情是 Pageable
实例在聚合 (MyDocument
) 上运行,而不是 "inside" myArray
字段,这就是我怀疑的原因我想以某种方式使用 $slice 运算符。
我怎样才能做到这一点?如果使用 ReactiveMongoRepository then I'm fine with using ReactiveMongoOperations.
无效我设法通过更改解决了这个问题:
interface MyRepository : ReactiveMongoRepository<MyDocument, String> {
fun findByMyId(myId: String, pageable: Pageable): Flux<MyArrayProjection>
}
至:
interface MyRepository : ReactiveMongoRepository<MyDocument, String> {
@Query(value = "{ '_id' : ?0 }", fields = "{ 'myArray': { '$slice': ?1 } }")
fun findByMyId(myId: String, slice: Array<Int>): Flux<MyArrayProjection>
}
然后调用它:
myRepository.findByMyId("1", arrayOf(1, 2))