title: How do I turn a Scala case class into a mongo Document
body: <p>I'd like to build a generic method for transforming Scala Case Classes to Mongo Documents.</p>
  
  <p>A promising <a href="http://mongodb.github.io/casbah/2.8/scaladoc/index.html#org.mongodb.scala.bson.collection.immutable.Document$" rel="nofollow">Document constructor</a> is</p>
  
  <pre><code>fromSeq(ts: Seq[(String, BsonValue)]): Document
  </code></pre>
  
  <p>I can turn a case class into a Map[String -> Any], but then I've lost the type information I need to use the implicit conversions to BsonValues. Maybe TypeTags can help with this?</p>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>import org.mongodb.scala.bson.BsonTransformer
  import org.mongodb.scala.bson.collection.immutable.Document
  import org.mongodb.scala.bson.BsonValue
  
  case class Person(age: Int, name: String) 
  
  //transform scala values into BsonValues
  def transform[T](v: T)(implicit transformer: BsonTransformer[T]): BsonValue = transformer(v)
  
  // turn any case class into a Map[String, Any]
  def caseClassToMap(cc: Product) = { 
    val values = cc.productIterator
    cc.getClass.getDeclaredFields.map( _.getName -&gt; values.next).toMap
  }
  
  // transform a Person into a Document
  def personToDocument(person: Person): Document = {  
    val map = caseClassToMap(person)
  
    val bsonValues = map.toSeq.map { case (key, value) =&gt;
      (key, transform(value))
    }
  
    Document.fromSeq(bsonValues)
  }
  
  &lt;console&gt;:24: error: No bson implicit transformer found for type Any. Implement or import an implicit BsonTransformer for this type.
             (key, transform(value))
  </code></pre>