Py学习  »  NoSql

如何在规范化的nosql数据库中上下引用多个级别?

Gergő Horváth • 4 年前 • 965 次点击  

想象一下这种关系结构:

planet
  continent
    country
      city

但我想创建四个不同的集合以使其正常化。

最好描述一下,它的模式应该是:

type Planet {
  planetName: String
}
type Continent {
  continentName: String
}
type Country {
  countryName: String
}
type City {
  cityName: String
}

type Query {
  planets: [Planet]
  continents: [Continent]
  countries: [Country]
  cities: [City]
}

太好了,我可以查询每个单独的元素,并得到所有的元素。

但是如果我想得到一个大陆上所有的城市呢?

我需要改进下一部分:

type Continent {
  continentName: String
  cities: [City]
}
type City {
  cityName: String
}
type Query {
  planets: [Planet]
  continents: [Continent]
  countries: [Country]
  cities: [City]
}

现在如果我们查询,我们会得到 continentName (身份证),我们可以得到所有属于它的城市。但我们需要在城市和大陆之间建立某种关系。我的想法类似于数据库结构:

continent: {
  continentName, //the id
  countries, //array of ids of countries
  cities //array of ids of cities
}

这样我就可以编写一个解析器来获取 cities 从查询的父对象,以及从指定id的cities集合查询。

但如果我想做这样的事呢:

type Query {
  cities(continentName: String): [City]
}

所以反过来,把所有属于欧洲大陆的城市都找来。我可以去查询特定的大陆,得到属于它的所有城市,并获取指定的城市,但这需要很长时间。

continent: {
  continentName, //the id
  planetName //id of the belonging planet
},
city: {
  cityName, //the id
  countryName, // id of the belonging country
  continentName, //id of the belonging continent
  planetName //id of the belonging planet
}

有了上面的结构,我可以连接,并以嵌套格式查询数据,无论从哪个级别向下多少级别,以及从哪个方向。这似乎是一个完美的解决方案,但正如我读过建模noSQL数据库,我还没有找到它的任何地方。

解决方案是否存在任何可能的瓶颈或问题?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55548
 
965 次点击  
文章 [ 1 ]  |  最新文章 4 年前
amirouche
Reply   •   1 楼
amirouche    4 年前

你想解决的问题很难。

  • 创建一个描述世界区域的集合,区域将与WGS84坐标(或行星特定坐标系)的几何形状相关联

  • 创建的集合 PointOfInterest 像城市一样,这些只是WGS84坐标(有点像经度和纬度)。

然后需要一种查询几何图形的交集、包含和并集的方法。

会涉及到一些动态编程,因为据我所知,没有一个坐标系可以在整个宇宙中工作。

研究PostGIS,GDAL和 https://github.com/sshuair/awesome-gis