0

I am working on java first time. database is MongoDB. and I am trying to extract data from MongoDB. following the link

Below is code for database query

MongoCollection<Person> collection = database.getCollection("people", Person.class);

BasicDBObject whereQuery = new BasicDBObject();

List<BasicDBObject> obj = new ArrayList<BasicDBObject>();

obj.add(new BasicDBObject("name","someName"));
obj.add(new BasicDBObject("age","someAge"));
whereQuery.put("$and", obj);

FindIterable<Person> iterDoc = collection.find(whereQuery);
MongoCursor<Person> it = iterDoc.iterator();

while (it.hasNext()) {  

  Person data = it.next();
  System.out.println(data);

}

I am using same POJO from link

when I run code, I get below output

com.test.pojo.document.Person@6572421

What does this output means and why it is not showing result. If I skip POJO and use it like below then it works fine shows result from database

MongoClient mongo = MongoClients.create("mongodb://localhost:27017");

MongoDatabase database = mongo.getDatabase("DBname");  

MongoCollection<Person> collection = database.getCollection("people");

BasicDBObject whereQuery = new BasicDBObject();

List<BasicDBObject> obj = new ArrayList<BasicDBObject>();

obj.add(new BasicDBObject("name","someName"));
obj.add(new BasicDBObject("age","someAge"));
whereQuery.put("$and", obj);

FindIterable<Person> iterDoc = collection.find(whereQuery);
MongoCursor<Person> it = iterDoc.iterator();

while (it.hasNext()) {  

  Person data = it.next();
  System.out.println(data);

}
hrishi
  • 1,610
  • 6
  • 26
  • 43

0 Answers0