0

I want to create different collection in mongodb for that i have a scala object class here is my code

object Factory {

val connectionMongo = MongoConnection(SERVER)
val collectionMongo = connectionMongo(DATABASE)("collectionA")

}

i want to add different collection names for that i am doing it like this object class here is my code

    object Factory {

    var COLLECTIONName:String=""
    def setCollectionName(name:String)
    {
      COLLECTIONName=name
    }
    val connectionMongo = MongoConnection(SERVER)
    val collectionMongo = connectionMongo(DATABASE)(COLLECTIONName)

    }

class testA {

//getting collection object 
Factory.setCollectionName("collectionA")
collectionMongo.find()//fetching the record of collectionA

}

class testB {

//getting collection object 
Factory.setCollectionName("collectionB")
collectionMongo.find()//fetching the record of collectionB
}

but this code is not working as desired it always gets COLLECTIONName value to empty string "" please guide me where i am doing wrong ,please help

4
  • make collectionMongo "lazy val" will work. Commented Jun 24, 2016 at 6:30
  • will it effect any performance ? Commented Jun 24, 2016 at 6:32
  • No. But I suggest you completely rewrite your code. Commented Jun 24, 2016 at 6:34
  • can you explain it via example i am having difficulty in understanding Commented Jun 24, 2016 at 6:35

2 Answers 2

1

This is a typical factory pattern, you can change your collectionMongo from a val into a function like this

object Factory {
    val SERVER = "<some server>"
    val DATABASE = "<some database>"
    val connectionMongo = MongoConnection(SERVER)(DATABASE)
    def getCollection(name: String) = connectionMongo(name)
}

Usage

class testA {
    val collectionA = Factory.getCollection(nameA)
}

class testB {
    val collectionB = Factory.getCollection(nameB)
}
Sign up to request clarification or add additional context in comments.

Comments

1

you have written

val COLLECTIONName:String=""

And then you are reassigning the value. You cannot reassign a value in val, instead of it use var. But that's a bad approach too.. So try to modify the code so that it must be written in a more functional way.

The modified code would be something like :

class Factory(connectionName:String) {
    val connectionMongo = MongoConnection(SERVER)
    val collectionMongo = connectionMongo(DATABASE)(connectionName)
}

class testA {
    //getting collection object 
    val factory=new Factory("collectionA")
    factory1.collectionMongo.find()//fetching the record of collectionA
}

class testB {
    //getting collection object 
    val factory=new Factory("collectionB")
    factory.collectionMongo.find()//fetching the record of collectionB
}

I am not sure if this is the best way but its totally a better way.

The singleton object Approach as asked:

object Factory {
    var connectionName:String=_
    val connectionMongo = MongoConnection(SERVER)
    def collectionMongo = connectionMongo(DATABASE)(connectionName)

    }

class testA {

//getting collection object 
 Factory.connectionName="collectionA"
Factory.collectionMongo.find()//fetching the record of collectionA

}

class testB {

//getting collection object 
 Factory.connectionName="collectionB"
Factory.collectionMongo.find()//fetching the record of collectionB
}

I hope that helps !

4 Comments

sorry its a typo i have edited it ,its actually var ...can you please explain a better approach with an example ?
I have updated the answer take a look ! :) And upvote ! :D
thanks but i want to use scala object because i need a singleton object ,thats why i am not using scala class
But using var and overiding the values is a bad approach AFAIK, and if you want to follow the singleton object approach then use var there, and check the updated code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.