2
I am using mongoDB 3.2 with Java.
You can save a mongoDB query to a list of objects in a class of mine?
I want to save the query directly on a list of people.
class Pessoa{
String nome;
Date dataNascimento;
String email;
}
I actually do this, because it’s the only way I know:
public List<Pessoa> listar() throws Exception {
List<Pessoa> listPessoa = new ArrayList<>();
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase db = mongoClient.getDatabase("dbExemploMongo");
FindIterable<Document> iterable = db.getCollection("colecao1").find();
for (Document document : iterable) {
listPessoa.add(new Pessoa(
document.getString("nome"),
document.getDate("datanasc"),
document.getString("email"))
);
}
return listPessoa;
}
Thanks in advance!
2 options. use Reflection for popular object based on attribute name. Use a Orm that already does it for you. I preferably don’t like orms so having to use data mappers.
– Vinicius Zaramella