0

I am storing a mutable Set in mongoDB and now i want to retrieve that set from mongo but i am un able to do that maybe i am doing it in a wrong way here is my code

class A{
var genreIdSet = scala.collection.mutable.Set[Int]()

def addToGenreIdSet(genreId : Int)  = {
    genreIdSet += genreId

  }

  def getGenreIdSet : scala.collection.mutable.Set[Int]= {
    genreIdSet
  }
}

for storing in mongo

val result:WriteResult= collection.insert(new BasicDBObject("_id",artistImpl.getUuid)
                        .append("GetGenreIdSet",artistImpl.getGenreIdSet)
                                            ,WriteConcern.Acknowledged)

and i am retrieving like this

val cursor=collection.find()
    var obj=new BasicDBObject
 try {
     while(cursor.hasNext)
     {
       obj=cursor.next().asInstanceOf[BasicDBObject]
       id=obj.getString("_id").toInt
       log.info("id value is "+id)
              var a =obj.get("GetGenreIdSet").asInstanceOf[scala.collection.mutable.Set[Int]]
       log.info("Set is "+a) 

but it throws an error

-com.mongodb.BasicDBList cannot be cast to scala.collection.mutable.Set
java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.mutable.Set

How can i resolve this issue please help me

swaheed
  • 3,671
  • 10
  • 42
  • 103

1 Answers1

1

There are no implicit convertors from BasicDBList to a Set.

The equivalent to a BasicDBList would be a List and there are helpers for MongoDBObjects that allow you get a key as a type eg:

obj.getAs[List[Integer]]("GetGenreIdSet")

getAs follow Scala convention and returns an Option here it would return Option[List[Integer]]

Ross
  • 17,861
  • 2
  • 55
  • 73