Py学习  »  NoSql

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

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

想象一下这种关系结构:

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
 
972 次点击  
文章 [ 1 ]  |  最新文章 4 年前