Home > Mobile >  Spring data MongoDB - Insert batch skip duplicates
Spring data MongoDB - Insert batch skip duplicates

Time:09-22

I want to save 1000 entries in batch, ignoring the duplicates

@Document("contacts")
@Data
@CompoundIndex(name = "unique_name_number", unique = true, def = "{'name' : 1, 'phoneNumber' : 1 }")
@AllArgsConstructor
public class ContactModel {

    @Id
    String id;

    Instant createdAt;
    String name;
    String phoneNumber;

}

And the execution

MongoTemplate mongo;
List<ContactModel> contacts = getLongList()
mongo.insert(contacts, ContactModel.class)

This code throws an exception if there are duplicates (name,phoneNumber).

How can I tell to mongo

Skip the duplicates and continue to insert the rest?

Thanks much

CodePudding user response:

You can try, using an unordered BulkOperations, like this:

BulkOperations bulkOps = mongo.bulkOps(BulkMode.UNORDERED, ContactModel.class);
bulkOps.insert(contacts);
BulkWriteResult result;
try {
   result = bulkOps.execute();
} catch(BulkOperationException e) {
   // exception triggers after the operation is completed in case of duplicates
   result = e.getResult();
}

The BulkOperationException also contains information about errors (and categories)

List<BulkWriteError> errors = e.getErrors();

  • Related