【问题标题】:Spring mongodb query nested array containing nested arraysSpring mongodb查询包含嵌套数组的嵌套数组
【发布时间】:2019-12-10 09:40:06
【问题描述】:

我有这 3 个课

@Document(collection = "Countries")
class CountryDoc
{
    @Id
    lateinit var id : String

    var name : String = ""

    var governorates : ArrayList<Governorate> = ArrayList()

}

class Governorate
{
    var id : String = ""

    var name : String = ""

    var cardinalPoint : CardinalPoint = CardinalPoint.SOUTH_WEST

    var cities : ArrayList<City> = ArrayList()
}

class City
{
    var id : String = ""

    var name : String = ""
}

我想得到一个城市的完整地址,结果一定是这样的

{
  "countryId" : ""
  "countryName" : "",
  "governorateId" : "",
  "governorateName" : "",
  "cardinalPoint" : ""
  "cityId" : "",
  "cityName" : ""
}

我试过这个查询但没有用,我只有国家的信息

    val countryCriteria = Criteria.where("id").`is`(countryId)
    val governorateCriteria = Criteria.where("id").`is`(governorateId)
    val cityCriteria = Criteria.where("id").`is`(cityId)

    val aggregation = Aggregation.newAggregation(
        Aggregation.match(countryCriteria),
        Aggregation.project().and("id").`as`("countryId").and("name").`as`("countryName"),
        Aggregation.unwind("governorates"),
        Aggregation.match(governorateCriteria),
 Aggregation.project().and("name").`as`("governorateName").and("id").`as`("governorateId").and("cardinalPoint").`as`("cardinalPoint")
        Aggregation.unwind("cities"),
        Aggregation.match(cityCriteria)
        Aggregation.project().and("name").`as`("cityName").and("id").`as`("cityId")
    )

val result = mongoDb.aggregate(aggregation, CountryDoc::class.java, Address::class.java).uniqueMappedResult

我不明白为什么这对我不起作用,有人可以帮助我吗?

【问题讨论】:

    标签: java mongodb kotlin spring-data


    【解决方案1】:

    你可以从我在 Mongoshell 上写的这个查询中得到启发:

    可以在Mongo playground上测试查询

    db.city.aggregate([
      {
        "$match": {
          "_id": "c1"
        }
      },
      {
        "$lookup": {
          "from": "governate",
          "as": "governates",
          "localField": "_id",
          "foreignField": "cities"
        }
      },
      {
        "$project": {
          "governate": {
            $arrayElemAt: [
              "$governates",
              0
            ]
          },
          "cityName": "$name"
        }
      },
      {
        "$lookup": {
          "from": "country",
          "as": "coutries",
          "localField": "governate._id",
          "foreignField": "governates"
        }
      },
      {
        "$project": {
          "country": {
            $arrayElemAt: [
              "$coutries",
              0
            ]
          },
          "cityName": 1,
          "governateName": "$governate.name",
          "governateId": "$governate._id"
        }
      },
      {
        "$project": {
          "_id": 0,
          "cityId": "$_id",
          "countryName": "$country.name",
          "countryId": "$country._id",
          "cityName": 1,
          "governateId": 1,
          "governateName": 1
        }
      }
    ])
    

    我使用的数据是这样的:

    db={
      "country": [
        {
          "_id": "1",
          "name": "country",
          "governates": [
            "g1"
          ]
        }
      ],
      "governate": [
        {
          "_id": "g1",
          "name": "governate",
          "cities": [
            "c1"
          ]
        }
      ],
      "city": [
        {
          "_id": "c1",
          "name": "city"
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 1970-01-01
      • 2015-08-13
      • 2012-05-04
      • 2011-07-12
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多