我试图在Java程序中将json导入到MongoDB中,并且遇到以下异常。 线程“主要” org.bson.BsonInvalidOperationException中的异常:仅在CurrentBSONType为DOCUMENT时才能调用readStartDocument,而在CurrentBSONType为ARRAY时不能调用
这是我的源代码。
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));
}
}
能不能让我知道我要去哪里错了?。