Created
April 16, 2014 08:06
-
-
Save sgodbillon/10829302 to your computer and use it in GitHub Desktop.
List Database command example (ReactiveMongo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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