DBCollection.insert chấp nhận một tham số kiểu DBObject , List<DBObject> hoặc một mảng DBObject s để chèn nhiều tài liệu cùng một lúc. Bạn đang chuyển vào một mảng chuỗi.
Bạn phải điền tài liệu theo cách thủ công (DBObject s), chèn chúng vào List<DBObject> hoặc một mảng DBObject s và cuối cùng là insert họ.
DBObject document1 = new BasicDBObject();
document1.put("name", "Kiran");
document1.put("age", 20);
DBObject document2 = new BasicDBObject();
document2.put("name", "John");
List<DBObject> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);
collection.insert(documents);
Đoạn mã trên về cơ bản giống với lệnh bạn sẽ phát hành trong MongoDB shell:
db.people.insert( [ {name: "Kiran", age: 20}, {name: "John"} ]);