score:0

Accepted answer

It seems like according to the sources it is not supported in reactivemongo. So I have created a quick helper:

def getAsByPath[T](path: String, doc: BSONDocument)
       (implicit reader: BSONReader[_ <: BSONValue, T]): Option[T] = {
  val pathChunks = path.split('.')
  var pathIndex = 0
  var curDoc: Option[BSONDocument] = Some(doc)
  var currentChunk = ""

  while(pathIndex != pathChunks.size) {
    currentChunk += pathChunks(pathIndex)

    // If this is the last chunk it must be a value
    // and if previous Doc is valid let's get it
    if (pathIndex == pathChunks.size - 1 && curDoc != None)
      return curDoc.get.getAs[T](currentChunk)

    val tmpDoc = curDoc.get.getAs[BSONDocument](currentChunk)
    if (tmpDoc != None) {
      currentChunk = ""
      curDoc = tmpDoc
    } else {
      // We need to support this as sometimes doc ID 
      // contain dots, for example "1.0"
      currentChunk += "."
    }

    pathIndex += 1
  }

  None
}

However my second question is still valid. If someone knows please let me know.


Related Query

More Query from same tag