Q:

Not able to upload JSON in MongoDB using Java

I am trying to upload JSON file in MongoDB using Documents, but getting the following error. I'm new to Mongo. Can I get any help?
Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY. 
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import java.io.*;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.InsertOneModel;
import org.bson.Document;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args)  {

        //System.out.println(new File("").getAbsolutePath());
        // Creating a Mongo client
        MongoClient client = new MongoClient("localhost", 27017);
        MongoDatabase database = client.getDatabase("mongotest");
        MongoCollection<Document> collection = database.getCollection("jsondata");

        int count = 0;
        int batch =100;

        List<InsertOneModel<Document>> docs =  new ArrayList<>();
        //System.out.println(docs.getClass().getName());
        try (BufferedReader br = new BufferedReader(new FileReader("mongotest.json"))) {
            String line;
            while ((line = br.readLine()) != null) {
            //    System.out.println(Document.parse(line));
                docs.add(new InsertOneModel<>(Document.parse(line)));
                count++;

            if (count == batch) {
                collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
                docs.clear();
                count = 0;
            }
            }
        }
            catch(FileNotFoundException e) {
                System.out.println("The file doesn't exist");
            }
            catch(IOException e){
                System.out.println("The file could not be read");
            }
    }
}

A:

Can you share the mongotest.json contents?
Each line must be an object (surrounded in {}), not an array (surrounded in []).
Edit:
To fix your issue, instead of reading using BufferedReader do this:
...
var json = Files.readString("mongotest.json", StandardCharsets.UTF_8);
var jsonarray = new JSONArray(json);
for (int i = 0; i < jsonarray.length(); i++) {
    var line = jsonarray.getJSONObject(i);
    docs.add(new InsertOneModel<>(Document.parse(line.toString())));
    count++;
}
...

