Reputation: 11
everyone?
I have a JSON with the following structure:
{
"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
}
}
}
}
The code above represents three examples of documents stored in my MongoDB.
Note that the keys (Recife, Cuiabá and Santos) are dynamic in this schema.
My question is how to map these dynamic keys in a Entity using Spring Data MongoDB? I tried the following approach:
@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;
}
But when the Spring Repository make the query, the map variable returns null.
What is wrong with my code?
Best regards.
Upvotes: 0
Views: 2298
Reputation: 6748
No you can't. If you want to map those documents to objects you should have static keys. Possible solution here is retrieve documents using simple operations:
Iterable<BasicDBObject> objects = mongoOperations.find(query, BasicDBObject.class, "collection");
And after that convert it to your object manually.
objects.stream()
.map(obj -> new Entity(...)
.collect(Collectors.toList());
Upvotes: 1