I want to use a custom String code as a primary key in my collection and ensure that Mongo Repository save updates/upserts the document based on code. But when a 2nd entry is added with same code, it throws E11000 duplicate key error collection. PFB my code snippet :
public class WebCategoryTemp {
@Id
private String code;
@Indexed
private Long timestamp;
private String name;
private String parent;
private Map<String,String> attributes;
private List<String> brands;
private List<String> categories;
private List<String> optionCodes;
}
Repository:
public interface WebCategoryTempRepository extends
MongoRepository<WebCategoryTemp, String> {
}
I am getting the below exception while adding another entry with same code :
org.springframework.dao.DuplicateKeyException: E11000 duplicate key error
collection: AQUAMAN.webCategoryTemp index: _id_ dup key: { _id:
"GlobalTrends" }; nested exception is com.mongodb.MongoWriteException:
E11000 duplicate key error collection: AQUAMAN.webCategoryTemp index: _id_
dup key: { _id: "GlobalTrends" }
Sample Mongo Document:
{
"_id" : "GlobalTrends",
"timestamp" : NumberLong("1570439407216"),
"name" : "GlobalTrendss",
"parent" : "83",
"attributes" : {
"categoryLevel" : "4",
"returnWindow" : "35",
"isReturnable" : "true",
"maximumQuantity" : "2",
"categoryToggle" : "toggleCode",
"catalogId" : "rilfnlProductCatalog"
},
"brands" : [ ],
"categories" : [
"830316001"
],
"version" : NumberLong(0)
}