I'm using mongoDB 3.2 with Java.
Is it possible to save a mongoDB query in a list of objects of a class of mine?
I want to save the query directly to a list of people.
class Pessoa{
String nome;
Date dataNascimento;
String email;
}
I'm currently doing 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;
}
Thank you in advance!