【问题标题】:MongoDB, Spring JDBC, Dynamic keys, MappingMongoDB、Spring JDBC、动态键、映射
【发布时间】:2017-04-19 09:25:49
【问题描述】:

我正在使用 spring jdbc 和 mongoDB。我将有如下文件。 一个像

{
    "_id" : ObjectId("58f49f05c88c8f2cb8061e12"),
    "temp" : {
        "27" : {
            "59" : "5"
        },
       "28":{
          "0":"0",
          "1":"1"
       }
    },
    "luminosity" : {
        "27" : {
            "59" : "3"
        }
    },
    "identifier" : 753    
}

另一个可能像

{
    "_id" : ObjectId("58f49f05c88c8f2cb8061e12"),
    "humidity" : {
        "27" : {
            "59" : "5"
        }
    },
    "identifier" : 753    
}

那么我该如何为此编写一个映射类。请帮忙! 我写过类似的东西

@Document(collection = "Data")
public class Data {

    private String identifier;


    /**
     * @return the identifier
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * @param identifier
     *            the identifierto set
     */
    public void setIdentifier(String identifier) {
        this.identifier= identifier;
    }


    }

如何为其他字段编写映射器?这是动态的

【问题讨论】:

  • 你的数据结构很奇怪。为什么会有一个名为“27”的物体,它代表什么?如果您无法更改数据模型,您可以将其加载为 json 对象或linkedtreemaps。
  • 只是想知道下面的答案是否有帮助?
  • @p.streef 数字代表分钟/秒。谢谢你的回复
  • 感谢@notionquest 的回复。它是部分使用完整的。部分是因为正如我提到的温度和光度不是固定的。可能我必须处理数据模型本身

标签: mongodb dynamic field spring-jdbc


【解决方案1】:

数据结构非常规。通常,尽管 MongoDB 支持 key 属性,但它不应该是非常动态的。当您尝试使用多个管道编写复杂的聚合查询时,它可能会变得复杂。

无论如何,给定上述结构,可以有多种方式来定义文档模型。我使用简单的 Java 类(Map 和 String)提供了以下解决方案。

之所以使用简单的 Map 结构,是因为大部分库都支持序列化过程,可以使用 Java 8 Lambda 函数来迭代并获取值。

文档类:-

@Document(collection = "weather")
public class Weather {

    @Id
    private String id;

    private Map<String, Map<String, String>> temp;

    private Map<String, Map<String, String>> luminosity;

    private String identifier;

    public String getId() {
        return id;
    }

    public Map<String, Map<String, String>> getTemp() {
        return temp;
    }

    public Map<String, Map<String, String>> getLuminosity() {
        return luminosity;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setTemp(Map<String, Map<String, String>> temp) {
        this.temp = temp;
    }

    public void setLuminosity(Map<String, Map<String, String>> luminosity) {
        this.luminosity = luminosity;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }
}

使用 Lambda 的示例代码:-

Weather weather = mongoOperations.findById(id, Weather.class);

        System.out.println(weather.toString());
        weather.getTemp().forEach((k, v) -> {
            System.out.println("Temp : " + k + "; Values : " + v);
        });
        weather.getLuminosity().forEach((k, v) -> {
            System.out.println("Temp : " + k + "; Values : " + v);
        });     
        System.out.println(weather.getIdentifier());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2018-04-26
    • 2010-10-28
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2013-07-27
    • 2013-07-31
    相关资源
    最近更新 更多