I am trying to populate the Java POJO class with the Mongo DB collection but getting null when trying to get data from the Pojo class
Here CSVRow is the POJO class name
The class constructor
MongoDatabase database;
MongoDbUtils() {
String uri = "mongodb://localhost:27017/?maxPoolSize=20&w=majority";
//This registry is required for your Mongo document to POJO conversion
ConnectionString connectionString = new ConnectionString(uri);
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings clientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(clientSettings);
database = mongoClient.getDatabase("StudyPriceDataTest");
}
Function to retrieve data from POJO
public MongoCollection<CsvRow> RetriveAllDocumentsInCSVRows(String collectionName) {
System.out.println(collectionName);
MongoCollection<CsvRow> collection = database.getCollection(collectionName, CsvRow.class);
List<CsvRow> csvRows = collection.find(new Document(), CsvRow.class).into(new ArrayList<CsvRow>());
for(CsvRow doc:csvRows) {
System.out.println("vinsds="+doc.getVin()); **<-- here I am getting null**
}
return collection;
}
I tried with the code mentioned and I am expecting to get the data from the POJO
We can use
public List<CsvRow> RetriveAllDocumentsInCSVRows(String collectionName) {
List<CsvRow> userList = new ArrayList<CsvRow>();
List<CsvRow> collection = database.getCollection(collectionName, CsvRow.class).find().into(userList);
// return all documents in the collection
for (CsvRow u : userList) {
System.out.println(u.getVin());
}
return collection;
}