Inserting documents
Let's insert some documents into our newly created database. We want to store information about GitHub users, using the following document structure:
{ id: <mongodb object id>, login: "pbugnion", github_id: 1392879, repos: [ { name: "scikit-monaco", id: 14821551, language: "Python" }, { name: "contactpp", id: 20448325, language: "Python" } ] }
Casbah provides a DBObject
class to represent MongoDB documents (and subdocuments) in Scala. Let's start by creating a DBObject
instance for each repository subdocument:
scala> val repo1 = DBObject("name" -> "scikit-monaco", "id" -> 14821551, "language" -> "Python") repo1: DBObject = { "name" : "scikit-monaco" , "id" : 14821551, "language" : "Python"}
As you can see, a DBObject
is just a list of key-value pairs, where the keys are strings. The values have compile-time type AnyRef
, but Casbah...