繁体   English   中英

如何测试MongoDB连接?

[英]How to test MongoDB connection?

如何测试MongoDB连接? 这是我的代码:

class MongoDB(val SERVER:String, val PORT:Int, val DATABASE: String, val     COLLECTION: String){

    def establishConnection(): MongoCollection = {

        val mongoClient = MongoClient(SERVER, PORT)
        println("MongoDB client connection: "+ mongoClient)
        val db_handle = mongoClient(DATABASE)
        println("Connected to DB : "+ DATABASE)
        println("Collections present are :")
        println(db_handle.collectionNames)
        assert (establishConnection.size > 0 )

        db_handle(COLLECTION)
    }

    def insert(collection : MongoCollection, document : MongoDBObject) : Unit =          {
        println(collection.insert(document))
    }

    def find(collection : MongoCollection) = {
        println("In find query() :")
        val cursor = collection.find()
        cursor.toList
    }

    def find(collection : MongoCollection, obj : MongoDBObject)  = {
        println("In find query() with condition:")
        val cursor = collection.find(obj)
        cursor.toList
    }

    def findOne(collection : MongoCollection) ={
        println("In findOne query() :")
        val cursor = collection.findOne()
        cursor.toList
    }

    def findOne(collection: MongoCollection, obj : MongoDBObject) ={
        println("In findOne query() with condition:")
        val cursor = collection.findOne(obj)
        cursor.toList
    }

    def update(collection : MongoCollection, query : MongoDBObject, update :   MongoDBObject) = {
        val result = collection.update(query, update) //update or findAndModify can   be used
        result
    }

    def delete(collection : MongoCollection, query : MongoDBObject) = {
        val result = collection.findAndRemove(query);
        result
    }
}

您可以使用用Java编写的嵌入式mongo ,它首先下载mongo并将其存储在主目录中(仅第一次下载),然后运行它,可以选择mongoDB版本,这是一个示例项目,如何从中使用它斯卡拉测试https://github.com/SimplyScala/scalatest-embedmongo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM