GraphLookup聚合问题[Spring Data Mongodb]

时间:2019-03-26 10:31:10

标签: spring spring-data-mongodb

我具有以下文档类别:


        63895 M total memory
        53405 M used memory
        33214 M active memory
        22057 M inactive memory
         7027 M free memory
            1 M buffer memory
         3462 M swap cache
        32767 M total swap
        20186 M used swap
        12581 M free swap
    325131257 non-nice user cpu ticks
         2520 nice user cpu ticks
     84144511 system cpu ticks
  68293556865 idle cpu ticks
     57668780 IO-wait cpu ticks
            0 IRQ cpu ticks
      6908474 softirq cpu ticks
            0 stolen cpu ticks
   2775858411 pages paged in
  65451762163 pages paged out
    458921124 pages swapped in
    192317734 pages swapped out
   4056818407 interrupts
   1004201759 CPU context switches
   1532076671 boot time
     23467769 forks

,我想获取从叶到根类别的类别列表。为此,我有以下代码:

data class Category(
    @Id override val id: String? = null,
    val name: String? = null,
    val parentId: ObjectId? = null,
    val description: String? = null,
    val coverImagePath: String? = null,
    val subCategoriesNum: Int = 0,
    val productsNum: Int = 0,
    override val createdDate: Instant? = null,
    override val lastModifiedDate: Instant? = null)

结果查询如下:

        val aggregation = Aggregation.newAggregation(
            Category::class.java,
            Aggregation
                .graphLookup("categories")
                .startWith("id") // PROBLEM WITH ID (MUST BE _ID)
                .connectFrom("parentId")
                .connectTo("id")
                .`as`("parents"))

        return mongoOperations.aggregate(aggregation, CategoryResult::class.java)

但是正确的查询应该是这样:

{
  "aggregate": "__collection__",
  "pipeline": [
    {
      "$graphLookup": {
        "from": "categories",
        "startWith": "$id",
        "connectFromField": "parentId",
        "connectToField": "id",
        "as": "parents"
      }
    }
  ]
}

如何获得正确的查询?另外,对于spring数据mongodb,我可以从字符串转换为objectid以便在parentId和id之间进行比较吗?

谢谢

0 个答案:

没有答案