Skip to content

Instantly share code, notes, and snippets.

@sgodbillon
Created April 16, 2014 08:06
Show Gist options
  • Save sgodbillon/10829302 to your computer and use it in GitHub Desktop.
Save sgodbillon/10829302 to your computer and use it in GitHub Desktop.
List Database command example (ReactiveMongo)
import reactivemongo.core.commands._
import reactivemongo.bson._
import reactivemongo.bson.DefaultBSONHandlers._
import reactivemongo.bson.BSONInteger
import reactivemongo.bson.BSONString
import scala.Some
// { listDatabases: 1 }
/*
listDatabases returns a document for each database
Each document contains a name field with the database name,
a sizeOnDisk field with the total size of the database file on disk in bytes,
and an empty field specifying whether the database has any data.
*/
object ListDatabases extends AdminCommand[List[Database]] {
object ResultMaker extends BSONCommandResultMaker[List[Database]] {
def apply(doc: BSONDocument) = {
CommandError.checkOk(doc, Some("listDatabases")).toLeft {
doc.getAs[BSONArray]("databases").get.values.map {
case db: BSONDocument =>
Database(
db.getAs[BSONString]("name").get.value,
db.getAs[BSONNumberLike]("sizeOnDisk").get.toLong,
db.getAs[BSONBooleanLike]("empty").map(_.toBoolean).getOrElse(false)
)
case _ =>
throw new RuntimeException("databases field is missing")
}.toList
}
}
}
def makeDocuments = BSONDocument("listDatabases" -> BSONInteger(1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment