0
大家好?如何在MongoDB实体上为Spring Data MongoDB映射JSON和动态键?
我有以下结构的JSON:
{
"Recife": {
"coordenadas": {
"upperLeft": {
"x": 0,
"y": 1000
},
"bottomRight": {
"x": 600,
"y": 500
}
}
}
}
{
"Cuiaba": {
"coordenadas": {
"upperLeft": {
"x": 0,
"y": 1000
},
"bottomRight": {
"x": 600,
"y": 500
}
}
}
}
{
"Santos": {
"coordenadas": {
"upperLeft": {
"x": 0,
"y": 1000
},
"bottomRight": {
"x": 600,
"y": 500
}
}
}
}
上面的代码代表了存储在我的MongoDB文档的三个例子。
请注意,关键字(Recife,Cuiabá和Santos)在此模式中是动态的。
我的问题是如何使用Spring Data MongoDB在实体中映射这些动态密钥?我尝试了以下方法:
@Id
private String id;
@Field
private Map<String, Cidades> map;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, Cidades> getMap() {
return map;
}
public void setMap(Map<String, Cidades> map) {
this.map = map;
}
但是,当Spring存储库进行查询时,映射变量返回null。
我的代码有什么问题?
此致敬礼。
你是什么意思:请注意,键(累西腓,Cuiabá和桑托斯)在这种模式下是动态的? –
嗨。这意味着这些键上的文字永远不会相同。换句话说,我将获得不同的值(“Recife”,“Cuiabá”和“Santos”而不是“城市”,“城市”和“城市”)。最好的祝福。 –