DuplicatedKeyException on entering some data in db

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.

Hi @Diogo_Valente
Welcome to the community forum!!

Can you please help me to understand the following in order to help you with a solution:

  1. Can you share the document you are trying to insert into the collection?
  2. The ID field for windspeed and precipitation is marked as unique according to the code snipped shared, can you please confirm that _id value for the newly inserted document is different from the one already present into the collection.

Let me know if you have any further questions.

Thanks
Aasawari

The precipitation and windspeed documents are somewhat similar:

Precipitation

{
	"data": [
	{
		"descClassPrecIntEN": "--",
		"descClassPrecIntPT": "---",
		"classPrecInt": "-99"
	}, {
		"descClassPrecIntEN": "No precipitation",
		"descClassPrecIntPT": "Sem precipitação",
		"classPrecInt": "0"
	}, {
		"descClassPrecIntEN": "Weak",
		"descClassPrecIntPT": "Fraco",
		"classPrecInt": "1"
	}, {
		"descClassPrecIntEN": "Moderate",
		"descClassPrecIntPT": "Moderado",
		"classPrecInt": "2"
	}, {
		"descClassPrecIntEN": "Strong",
		"descClassPrecIntPT": "Forte",
		"classPrecInt": "3"
	}	]
}

windspeed

{
	“data”: [
	{
		“descClassWindSpeedDailyEN”: “–”,
		“descClassWindSpeedDailyPT”: “—”,
		“classWindSpeed”: “-99”
	}, {
		“descClassWindSpeedDailyEN”: “Weak”,
		“descClassWindSpeedDailyPT”: “Fraco”,
		“classWindSpeed”: “1”
	}, {
		“descClassWindSpeedDailyEN”: “Moderate”,
		“descClassWindSpeedDailyPT”: “Moderado”,
		“classWindSpeed”: “2”
	}, {
		“descClassWindSpeedDailyEN”: “Strong”,
		“descClassWindSpeedDailyPT”: “Forte”,
		“classWindSpeed”: “3”
	}, {
		“descClassWindSpeedDailyEN”: “Very strong”,
		“descClassWindSpeedDailyPT”: “Muito forte”,
		“classWindSpeed”: “4”
	} ]
}

And the forecast I’m using looks like this:

{
	“forecastDate”: “2022-04-22”,
	“data”: [
	{
		“tMin”: 9,
		“tMax”: 14,
		“classWindSpeed”: 2,
		“classPrecInt”: 2,
		“globalIdLocal”: 1010500,
	},
	{
		“tMin”: 8,
		“tMax”: 13,
		“classWindSpeed”: 2,
		“classPrecInt”: 2,
		“globalIdLocal”: 1020500,
	},
	…
	],
	“dataUpdate”: “2022-04-22T13:31:05”
}

So my idea, from the beginning, was to reference the classPrecInt from the forecast data to the classPrecInt from the precipitation list. The precipitation and windspeed are not supposed to change because it represents the severity of the weather.
From the exception given I understand that I’m maybe duplicating values and I just want to reference the collections.

After I investigated the issue I detected something. Everything is working fine except the precipitation even if the implementation is the same as the windspeed.
When I checked the collection, the id 0 shows “Document 2”:

image

Update:

In the precipitation object I changed the id type from int to Integer and for some reason it worked:

@Document("precipitation")
public class Precipitation {
    @Id
    private Integer id;
    private String descriptionPt;
    private String descriptionEn;

    public Precipitation(PrecipitationDao precDao) {
        this.id = Integer.parseInt(precDao.getClassPrecInt());
        this.descriptionPt = precDao.getDescClassPrecIntPT();
        this.descriptionEn = precDao.getDescClassPrecIntEN();
    }
}

in the collections still appears “document 2” in id 0 but it works.