public class test {
public static void main(String[] args) {//java连接mongodb数据库首先要连admin数据库,验证权限,选择服务器的IP以及端口才行。
MongoClient mongoClient = new MongoClient("127.0.0.1",27017);
DB db = mongoClient.getDB("Cnblogs");
// 获得表
DBCollection foo = db.getCollection("NoSql");
// 查询数据
DBCursor cur = foo.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
//连接数
CommandResult stats=db.command("serverStatus");
System.out.println(stats);
System.out.println("################");
System.out.println(stats.get("connections"));
System.out.println("################");
System.out.println(((HashMap)stats.get("connections")).get("current"));
}
}