Scala: How to insert documents into MongoDB with insert, save, and +=

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 16.4, “How to insert documents into MongoDB with insert, save, and +=.”

Problem

You want to save documents to a MongoDB collection from a Scala application.

Solution

To save/insert documents into a MongoDB collection with Scala, use the insert, save, or += methods of the Casbah MongoCollection class.

To save a document to your MongoDB collection, you can use the MongoCollection’s insert method:

collection.insert(buildMongoDbObject(apple))
collection.insert(buildMongoDbObject(google))
collection.insert(buildMongoDbObject(netflix))

You can also use the save method:

collection.save(buildMongoDbObject(apple))
collection.save(buildMongoDbObject(google))
collection.save(buildMongoDbObject(netflix))

And you can also use the += method:

collection += buildMongoDbObject(apple)
collection += buildMongoDbObject(google)
collection += buildMongoDbObject(netflix)
collection += buildMongoDbObject(amazon)

The intention of the insert and save methods is obvious; you’re inserting/saving data to your MongoDB collection. The third approach is a little different; it looks like what you’re doing is adding an object to a collection. In fact, you’re saving your object to the database collection with each += call.

Here’s what the += approach looks like in a complete program:

import com.mongodb.casbah.Imports._
import Common._

object Insert2 extends App {
    val collection = MongoFactory.collection

    // create some Stock instances
    val apple = Stock("AAPL", 600)
    val google = Stock("GOOG", 650)
    val netflix = Stock("NFLX", 60)
    val amazon = Stock("AMZN", 220)

    // add them to the collection (+= does the save)
    collection += buildMongoDbObject(apple)
    collection += buildMongoDbObject(google)
    collection += buildMongoDbObject(netflix)
    collection += buildMongoDbObject(amazon)
}

To use the insert or save methods, simply replace the += lines with their equivalent lines.

Discussion

If you’d like to experiment with this code, just add it to the SBT project that you started in Recipe 16.3. The buildMongoDbObject method in the Common class of that recipe converts a Scala object to a MongoDBObject that can be saved to the database using save, insert, or +=.

When choosing between save, insert, or +=, there’s obviously a big difference in style between += and the other methods. The save and insert methods accept a variety of different parameters and both return a WriteResult, so you have a number of options to choose from.

You’ll encounter the WriteResult and WriteConcern classes while working with the Casbah driver. These classes come from the MongoDB Java driver, which Casbah wraps. WriteResult lets you access the results of the previous write, and has methods like getField, getError, and getLastError.

WriteConcern provides options to let you control the write behavior, including behavior about network errors, slaves, timeouts, and forcing fsync to disk.

See Also