Hi there!
Some days ago, I started a new personal project based on getting data from a forecast API, converting to my desire and store for later uses.
To get all the information I have to make four requests:
- Regions → to get the name from the globalId
- Precipitations → to get the description from the id
- WindSpeed → to get the description from the id
- DailyForecast → to get the data and the id’s for the other requests
The classes are something like this:
@Document("region")
public class Region {
@Id
private int globalIdLocal;
private String name;
}
@Document("precipitation")
public class Precipitation {
@Id
private int id;
private String descriptionEn;
private String descriptionCh;
}
@Document("windspeed")
public class WindSpeed {
@Id
private int id;
private String descriptionEn;
private String descriptionCh;
}
@Document("dailyforecast")
public class DailyForecastDto {
@Id
private String id;
@DBRef
private Region region;
private int minTemp;
private int maxTemp;
@DBRef
private WindSpeed windSpeed;
@DBRef
private Precipitation precipitation;
private String dataUpdate;
private String forecastDate;
}
When I save the data for day 1, it saves it fine but when I try to save day 2 it shows:
org.springframework.dao.DuplicateKeyException: Write operation error on server localhost:27017. Write error: WriteError{code=11000, message=‘E11000 duplicate key error collection: weather-forecast-db.precipitation index: id dup key: { _id: 0 }’, details={}}.; nested exception is com.mongodb.MongoWriteException: Write operation error on server localhost:27017. Write error: WriteError{code=11000, message=‘E11000 duplicate key error collection: weather-forecast-db.precipitation index: id dup key: { _id: 0 }’, details={}}.
In my database it shows all the collections just fine (dailyforecast,precipitation,region,windspeed) and in my dailyforecast, the day 1 shows like it should:
{
"_id": "2310300.2022-04-22.2022-04-22T13:31:05",
"region": {
"$ref": "region",
"$id": 2310300
},
"minTemp": 16,
"maxTemp": 21,
"windSpeed": {
"$ref": "windspeed",
"$id": 2
},
"precipitation": {
"$ref": "precipitation",
"$id": 2
},
"dataUpdate": "2022-04-22T13:31:05",
"forecastDate": "2022-04-22",
"_class": "io.github.valvula.weather_forecast.dto.DailyForecastDto"
}
I don’t really know if I have some problem with the id’s of the windspeed and precipitation being the same but that I can’t change, or if I am doing some relation the wrong way.
I would appreciate if someone could enlighten me so I can understand my flaws.