I am working on inserting a CSV file into MongoDB. First I am converting my CSV into Json formatted array (Reference: https://dzone.com/articles/how-to-convert-csv-to-json-in-java) and then trying to upload it into MongoDB, but facing the below error(readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.):
Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.
at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:692)
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:724)
at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:452)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:148)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)
at org.bson.Document.parse(Document.java:105)
at org.bson.Document.parse(Document.java:90)
at com.ucm.json.ConnectMongoDB.connectToMongoDB(ConnectMongoDB.java:52)
at com.ucm.json.Main.main(Main.java:15)
My JSON string(result) looks like below:
[ {
"query" : "ecn",
"type" : "KeywordMatch",
"url" : "http://insidedell.com/ecn",
"description" : "ECN"
}, {
"query" : "product marketing",
"type" : "PhraseMatch",
"url" : "http://dellemc.com/product",
"description" : "Products"
}, {
"query" : "jive",
"type" : "ExactMatch",
"url" : "http://test.com/jive",
"description" : "Jive test"
} ]
Below is my code: Step 1: Convert CSV into JSON format string array
public class CreateJSON {
public String query;
public String type;
public String url;
public String description;
String result ;
public CreateJSON() {
}
public CreateJSON(String query,String type,String url,String description) {
this.query = query;
this.type = type;
this.url = url;
this.description = description;
}
public String createJsonFromCSV() throws IOException{
String csvFile = "C:\\Projects\\frontKeymatch_default_frontend.csv";
List<CreateJSON> createObjects = null;
Pattern pattern = Pattern.compile(",");
try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
createObjects = in.lines().map(line ->{
String[] x = pattern.split(line);
return new CreateJSON(x[0],x[1],x[2],x[3]);
}).collect(Collectors.toList());
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
result = mapper.writeValueAsString(createObjects);
}
return result;
}
}
Step 2) Connect to MongoDB and insert the data
public class ConnectMongoDB{
public void connectToMongoDB(String resultFromCSV) throws JsonGenerationException, JsonMappingException, IOException {
MongoClient mongo = new MongoClient( "localhost" ,27017);
Document doc = Document.parse(resultFromCSV);
mongo.getDatabase("db").getCollection("collection").insertOne(doc);
System.out.println("success");
}
}
Step 3: My main method:
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
CreateJSON upload = new CreateJSON();
ConnectMongoDB mongo = new ConnectMongoDB();
mongo.connectToMongoDB(upload.createJsonFromCSV());
}
}
Any help is appreciated. Thanks