要在Java中使用Mongo数据库
首先导入驱动包mongo-java-driver.jar。
然后获得库,获得集合。就可以对数据库操作了,比如:
//创建MongoClient对象 MongoClient mc=new MongoClient("127.0.0.1"); //获得test库 DB db = mc.getDB("test"); //获得user集合 DBCollection collection = db.getCollection("user"); //查询 DBCursor find = collection.find(); //输出数据 while(find.hasNext()){ DBObject txt = find.next(); Double object =(Double)txt.get("uid"); System.out.println(object); String str=(String)txt.get("uname"); System.out.println(str); } mc.close();