코틀린 몽고 디비 접속 확인 소스
import com.mongodb.MongoClient
import com.mongodb.ServerAddress
object mongo {
@JvmStatic
fun main(args: Array
val MongoDB_IP = "10.10.10.1"
val MongoDB_PORT = 27017
val DB_NAME = "admin"
//Connect to MongoDB
val mongoClient = MongoClient(ServerAddress(MongoDB_IP, MongoDB_PORT))
//View Database List
val databases = mongoClient.databaseNames
println("=====Database List===== ")
var num = 1
for (dbName in databases) {
println(num.toString() + ". " + dbName)
num++
}
println()
//Connect Database and Show Collection List in Database
val db = mongoClient.getDB(DB_NAME)
val collections = db.collectionNames
println("Database : $DB_NAME")
for (colName in collections) {
println(" + Collection: $colName")
}
}
}
결과
=====Database List=====
1. admin
2. config
3. local
Database : admin
+ Collection: system.users
+ Collection: system.version
Process finished with exit code 0