我是第一次使用Java。数据库是MongoDB。我正在尝试从MongoDB中提取数据。跟随link
下面是数据库查询的代码
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);
}
我正在通过链接使用相同的POJO
运行代码时,我得到以下输出
com.test.pojo.document.Person@6572421
此输出表示什么以及为什么不显示结果。如果我跳过POJO并按如下所示使用它,则可以正常显示数据库结果
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);
}