//方式2:使用mongodb api來操做mongodb數據庫。mongoClient是spring-mongo.xml裏面配的對象
String host = mongoClient.getAddress().getHost().toString();
DB db = mongoClient.getDB("invest");
//獲取到一個叫作"stock_quotation"的集合,至關於關係型數據庫中的"表"
DBCollection stockQuotationColl = db.getCollection("stock_quotation");
//find方法查詢全部的數據並返回一個遊標對象.意思其實就是多少行的數據,每個遊標就是一行
DBCursor cursor = stockQuotationColl.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
//獲取表stock_quotation的數據總條數
int sum = cursor.count();
System.out.println("sum==="+sum);
//只查詢第一條數據
DBObject oneStock_quotation = stockQuotationColl.findOne();
System.out.println("oneStock_quotation:" + oneStock_quotation);
//條件查詢id=50a1ed9965f413fa025166db
DBObject sq01 = stockQuotationColl.findOne(new BasicDBObject("_id",new ObjectId("5b03d024182fbb21a03955e4")));
System.out.println("sq01:" + sq01);
//條件查詢stockno=600000 ,trading_day=2008-04-25 00:00:00.000+08:00
DBObject sq02 = stockQuotationColl.findOne(new BasicDBObject("stockno","600000"));
System.out.println("sq02:" + sq02);
//得到全部collection
Set<String> collectionNames = db.getCollectionNames();
for(String name:collectionNames) {
System.out.println("collectionName:"+name);
}
System.out.println("host============" + host);spring