在mongodb中存储温度数据的最佳方法

在mongodb中存储温度数据的最佳方法,mongodb,Mongodb,我有一个关于温度数据的问题: 使用mysql,我存储的温度数据如下: city_id;city_name;month;min_temperature,max_temperature 123,New York,1,7,9 { "_id" : 20480, "name": "New York" "climate" : { "air" : { "1" : { "min" : "1", "max" : "8" },

我有一个关于温度数据的问题: 使用mysql,我存储的温度数据如下:

city_id;city_name;month;min_temperature,max_temperature
123,New York,1,7,9
{
  "_id" : 20480,
  "name": "New York"
  "climate" : {
    "air" : {
      "1" : {
        "min" : "1",
        "max" : "8"
      },
      "2" : {
        "min" : "2",
        "max" : "8"
      },
      "3" : {
        "min" : "3",
        "max" : "10"
      },
      "..."
    }
  }
}
{
      "_id" : 20480,
      "name": "New York"
      "climate" : {
        "air" : {
          "january" : {
            "min" : "1",
            "max" : "8"
          },
          "february" : {
            "min" : "2",
            "max" : "8"
          },
          "march" : {
            "min" : "3",
            "max" : "10"
          },
          "..."
        }
      }
    }
现在我想转到mongodb,并希望存储面向对象的数据文档 现在的结构如下所示:

city_id;city_name;month;min_temperature,max_temperature
123,New York,1,7,9
{
  "_id" : 20480,
  "name": "New York"
  "climate" : {
    "air" : {
      "1" : {
        "min" : "1",
        "max" : "8"
      },
      "2" : {
        "min" : "2",
        "max" : "8"
      },
      "3" : {
        "min" : "3",
        "max" : "10"
      },
      "..."
    }
  }
}
{
      "_id" : 20480,
      "name": "New York"
      "climate" : {
        "air" : {
          "january" : {
            "min" : "1",
            "max" : "8"
          },
          "february" : {
            "min" : "2",
            "max" : "8"
          },
          "march" : {
            "min" : "3",
            "max" : "10"
          },
          "..."
        }
      }
    }
有没有人对这种结构有过一些经验,或者有什么建议可以优化这种结构? 这种结构有意义吗?还是这样做更好:

city_id;city_name;month;min_temperature,max_temperature
123,New York,1,7,9
{
  "_id" : 20480,
  "name": "New York"
  "climate" : {
    "air" : {
      "1" : {
        "min" : "1",
        "max" : "8"
      },
      "2" : {
        "min" : "2",
        "max" : "8"
      },
      "3" : {
        "min" : "3",
        "max" : "10"
      },
      "..."
    }
  }
}
{
      "_id" : 20480,
      "name": "New York"
      "climate" : {
        "air" : {
          "january" : {
            "min" : "1",
            "max" : "8"
          },
          "february" : {
            "min" : "2",
            "max" : "8"
          },
          "march" : {
            "min" : "3",
            "max" : "10"
          },
          "..."
        }
      }
    }
任何帮助都会很好。

任何一个都会很好。 或者,您也可以选择:

"air" : [{min:1,max:5},{min:2,max:6}, {min:5,max:15} ... ]
改用数组。当然,0=1月

这取决于您是在climate.air.january中执行更多操作,还是在climate.air[0]中执行循环并执行更多操作
  • 这个结构看起来不错
  • :

    这在文档中明确了数据的月份,使用“1”、“2”等将为您节省一些磁盘空间,因为mongoDb键会为每个记录重复使用空间。 但由于您的数据不是数百万个文档,因此可以使用“一月”、“二月”。不过,您可能希望使用“jan”、“feb”、“mar”来保持关键字名称的简短和描述性

    • 现在,您需要考虑对数据进行何种查询。例如

      a) 哪个城市在三月份的气温最高?
      b) 哪些城市的最高气温在8到12之间

      c) 哪个城市全年平均最低气温最高?等

    通过直接查询和适当的索引,您将获得一些结果,但可能需要使用map reduce来满足高级数据需求。
    希望这有帮助。

    谢谢你的回答。但是,使用您提到的阵列结构是否有一些优势?