java链接用户名密码mongodb



MongoCredential credential = MongoCredential.createCredential("用户名", "数据库名称", "密码".toCharArray());
            MongoClient mongoClient = new MongoClient(new ServerAddress("IP", 端口号), Arrays.asList(credential));
            System.out.println("Connect to database successfully");
            DB database = mongoClient.getDB("数据库名称");//获取数据库
            DBCollection collection = database.getCollection(表名);//集合名
            BasicDBObject docss = new BasicDBObject();

你可能感兴趣的:(mongodb)