Note that there are some explanatory texts on larger screens.

plurals
  1. POApproach to read data from mongodb using reactive mongo irrespective of the structure
    text
    copied!<p>I'm using reactivemongo.</p> <p>While reading document from mongodb I have written code specific to the structure.</p> <h2>Structure</h2> <pre><code>{ "name" : "test", "age" : 3 } </code></pre> <p>For reading this I am using code like : </p> <pre class="lang-scala prettyprint-override"><code>val cursor = collection.find(query).cursor[BSONDocument] cursor.enumerate.apply(Iteratee.foreach { doc =&gt; var name: BSONDocument = doc.getAs[String]("name").get var age: BSONDocument = doc.getAs[Int]("age").get } </code></pre> <p>So now if later on, BSON structure gets changed like:</p> <pre><code>{ "name" : { firstName : "fname", lastName : "lname" }, "age" : 3 } </code></pre> <h2>So now I have to change my code for reading it</h2> <pre class="lang-scala prettyprint-override"><code>val cursor = collection.find(query).cursor[BSONDocument] cursor.enumerate.apply(Iteratee.foreach { doc =&gt; 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 } </code></pre> <p>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).</p> <p>While reading document with old structure an exception "None.get" is thrown, because as per the read method "name" should be a BSONDocument.</p> <p>What should be my approach to handle this issue??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload