Error while inserting an array of JSON documents in to MongoDB using Java Error while inserting an array of JSON documents in to MongoDB using Java mongodb mongodb

Error while inserting an array of JSON documents in to MongoDB using Java


You should either insert one by one or create a List of documents and use insertMany()

Here's an example:

MongoClient mongoClient = new MongoClient("GSI-547576", 27017);MongoDatabase database = mongoClient.getDatabase("test");MongoCollection < Document > table = database.getCollection("fc");FreightCompanyDao freightCompanyDao = new FreightCompanyDaoImpl(DataSourceFactory.getDataSource(DatabaseType.POSTGRES.name()));List < FreightCompany > freightCompanies = freightCompanyDao.getAllFreightCompanies();for (FreightCompany company: freighetCompanies) {    Document doc = Document.parse(GenericUtils.toJson(company))    collection.insertOne(doc)}