0

I have a spring-boot application which has the service to process and save a list of around 100 objects in a go.

Everythings seems fine till the processing state but calling save method of MongoRepository throws following exception:

com.mongodb.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error collection: uhtableau_db.ticket index: _id_ dup key: { : 0 }'

I retried after cleaning the db as well but same error. Unable to understand what am I doing incorrectly.

Here is the document object:

@Document
public class Ticket {

public Ticket(){

}

@Id
private long id;


private long ticket_id;  // this is the field in the json which i am processing


private String topic;

private List<String> tag;


private String type;


private long brand;
private long group;


private String priority;


private String status;



private String created_at;


private String channel;

}

I create a list of this Ticket document after processing some json data as input. I need to save this list in mongo.

Here is what am doing:

 @Autowired
 TicketRepository ticketRepository;

@GetMapping("/tickets")
public void saveTicketData(){

    List<Ticket> tickets = ZendeskClient.parseJsonForTicketList(ticketJson);
    ticketRepository.save(tickets);
}

And TicketRepository:

public interface TicketRepository extends MongoRepository<Ticket, Long> {

}
roger_that
  • 9,493
  • 18
  • 66
  • 102
  • 1
    I expect you save the 'id' field set to the database. This seems to be the same value all time. You may need to annotate some generator or set unique ids by yourself? – Konrad Sep 07 '18 at 06:57
  • I agree with what @Konrad said. try creating your own unique id or leave it as blank/null. mongo will take care of creating new id for you – VijayaKumar Thangavel Sep 11 '18 at 16:51

1 Answers1

2

You can use the corresponding object for @Id if you need the ability to use null values (Used by spring data mongo to make new inserts)

@Id
private Long id

Surely the "parseJsonForTicketList" method in parsing phase set the id with value 0.

Marco
  • 741
  • 3
  • 18