Approach to read data from mongodb using reactive mongo irrespective of the structure

StackOverflow https://stackoverflow.com/questions/18911110

  •  29-06-2022
  •  | 
  •  

Question

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??

Was it helpful?

Solution

This is a typical migration issue. And there are several ways to deal with migration. Typical solutions are:

  1. Migrate all existing documents in the database to fit your new format (either using the mongo shell, scripts or you can even include it in your application on startup.). There is currently no "auto migration" plugin for mongo available that does the job for you (I am working on one, but it is not yet finished).

  2. Make your application code aware of different schema versions. This means writing your BSONDocument parsing logic in a way that it accepts both versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top