0

jdbcTemplate.batchUpdate("INSERT INTO TABLE_NAME (COL1, COL2, COL3) VALUES (?, ?, ?)", List<Object[]>);

Is there a similar way that I can do same insert using MongoRepository Interfaces in Spring data mongodb and java ?

deadshot
  • 8,881
  • 4
  • 20
  • 39
Vamsi
  • 139
  • 1
  • 3
  • 12
  • this will help https://stackoverflow.com/questions/26657055/spring-data-mongodb-and-bulk-update – deadshot Feb 27 '20 at 07:24
  • is there a way to batch insert ? insert multiple records without any conditional updates. Just basic insert for a list of records. (say 100 records/objects in mongodb into a single Document) – Vamsi Feb 27 '20 at 07:32
  • this will help https://stackoverflow.com/questions/50718892/java-mongodb-save-multiple-documents-at-once – deadshot Feb 27 '20 at 08:49
  • 2
    Does this answer your question? [Java MongoDB save multiple documents at once](https://stackoverflow.com/questions/50718892/java-mongodb-save-multiple-documents-at-once) – deadshot Feb 27 '20 at 08:53

1 Answers1

0

You can insert multiple documents into the collection using one of the following methods. Assuming a test collection and a Test.java POJO representng a document:

(i) Inserting and Deleting using the MongoRepository:

@Repository
public interface MyRepository extends MongoRepository<Test, String> {
    void deleteByIdIn(List<ObjectId> ids);
}

// Run the saveAll repository method:

Test t1 = new Test("apple");
Test t2 = new Test("banana");
Test t3 = new Test("orange");
List<Test> ts = repository.saveAll(Arrays.asList(t1, t2, t3));

From the Mongo Shell lookup the newly inserted documents in the test collection:

> db.test.find()
{ "_id" : ObjectId("5e579153c4d0e55b6ad5f869"), "name" : "apple", "_class" : "com.example.demo.Test" }
{ "_id" : ObjectId("5e579153c4d0e55b6ad5f86a"), "name" : "banana", "_class" : "com.example.demo.Test" }
{ "_id" : ObjectId("5e579153c4d0e55b6ad5f86b"), "name" : "orange", "_class" : "com.example.demo.Test" }


// Perform Delete by ids:

ObjectId o1 = new ObjectId("5e579153c4d0e55b6ad5f869"); // apple
ObjectId o2 = new ObjectId("5e579153c4d0e55b6ad5f86b"); // orange
List<ObjectId> ids = Arrays.asList(o1, o2);
repository.deleteByIdIn(ids);

The objects with names "apple" and "orange" will be deleted. Also, note that the Test.java POJO class must have the id field defined to refer and delete by that field.


(ii) Using MongoTemplate:

MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "test");
BulkOperations bulkOps = mongoOps.bulkOps(BulkOperations.BulkMode.UNORDERED, Test.class);

Test t1 = new Test("orange");
Test t2 = new Test("grape");
bulkOps.insert(Arrays.asList(t1, t2));
BulkWriteResult r = bulkOps.execute();


(iii) The POJO Test Class:

public class Test {

    private String id;
    private String name;

    public Test(String name) {
        this.name = name;
    }

    public String getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    @Override
    public String toString() {
        return this.name;
    }
}
prasad_
  • 12,755
  • 2
  • 24
  • 36
  • Thanks! repository.save() is working fine for above requirement. How can i delete a list of items using a list of ids using custom query or in built methods ? List idList; the list of items associated with above list of ids (idList) should be deleted? – Vamsi Feb 28 '20 at 07:59
  • I added the `deleteByIdIn(List ids)` to the _repository_ object. Note the POJO class must define the `id` field for this functionality. See the _updated_ code. – prasad_ Feb 28 '20 at 10:35