I am trying to import a json into MongoDB in a java program and I am getting the following exception. Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY
This is my source code.
MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase database = client.getDatabase("test2");
MongoCollection<Document> collection =
database.getCollection("collection1");
int count = 0;
int batch = 100;
List<InsertOneModel<Document>> docs = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader("C:\\\\CsvFiles\\OneMillion.json"))) {
String line;
while((line = br.readLine()) != null) {
docs.add(new InsertOneModel<>(Document.parse(line)));
count++;
if(count == batch) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
docs.clear();
count = 0;
}
}
}
if(count > 0) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
}
}
Can you please let me know where I am going wrong?.
docs.add(new InsertOneModel<>(Document.parse(line)));toDocument document = Document.parse(String.format("{\"a\": %s}", line));docs.add(new InsertOneModel<>(document.get("a")))docs.add(new InsertOneModel<>(document.get("a")))withfor(Document doc:document.get("a")) {docs.add(new InsertOneModel<>(doc));}