I'm using reactivemongo.
While reading document from mongodb I have written code specific to the structure.
Structure
{
"name" : "test",
"age" : 3
}
For reading this I am using code like :
val cursor = collection.find(query).cursor[BSONDocument]
cursor.enumerate.apply(Iteratee.foreach { doc =>
var name: BSONDocument = doc.getAs[String]("name").get
var age: BSONDocument = doc.getAs[Int]("age").get
}
So now if later on, BSON structure gets changed like:
{
"name" : {
firstName : "fname",
lastName : "lname"
},
"age" : 3
}
So now I have to change my code for reading it
val cursor = collection.find(query).cursor[BSONDocument]
cursor.enumerate.apply(Iteratee.foreach { doc =>
var name: BSONDocument = doc.getAs[BSONDocument]("name").get
var fname : String = name.getAs[String]("firstName").get
var lname : String = name.getAs[String]("lastName").get
var age: BSONDocument = doc.getAs[Int]("age").get
}
I want to keep the data, which is currently using the old structure (i.e "name" as string) and insert new data using the new structure (i.e "name" as BSONDocument).
While reading document with old structure an exception "None.get" is thrown, because as per the read method "name" should be a BSONDocument.
What should be my approach to handle this issue??