MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
System.out.print("Conencton Successfull");
List<String> dbs = mongoClient.getDatabaseNames();
System.out.println(dbs);
System.out.println("database selected");
DB db = mongoClient.getDB("ddbb");
Set<String> collections = db.getCollectionNames();
for(String coll : collections){
System.out.println(coll);
}
运行上面的代码,给我可用的数据库名称,但不输出集合名称。我可以通过手动选择Collection来插入文档。
输出:
Conencton[admin, ddbb, local]
database selected
POM:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</dependency>