Scala: How to delete documents in a MongoDB with Casbah

This is an excerpt from the Scala Cookbook. This is Recipe 16.8, “How to delete documents in a MongoDB with Casbah.”

Problem

You want to delete one or more documents in a MongoDB collection in a Scala application.

Solution

Use the findAndRemove method of the Casbah MongoCollection class to delete one document at a time, or use the remove method to delete one or more documents at a time.

The following code uses findAndRemove to delete the document whose symbol field is AAPL:

val query = MongoDBObject("symbol" -> "AAPL")
val result = collection.findAndRemove(query)
println("result: " + result)

When a document is deleted, the findAndRemove method returns the document that was deleted, wrapped in a Some:

result: Some({ "_id" : { "$oid" : "50255d1d03644925d83b3d07"}, "symbol" : "AAPL" , "price" : 600.0})

If nothing is deleted, such as when you try to delete a document that doesn’t exist, the result is None:

result: None

Therefore, you’ll probably want to handle this using a match expression, as shown in the previous recipe.

To delete multiple documents from the collection, specify your search criteria when using the remove method, such as deleting all documents whose price field is greater than 500:

collection.remove("price" $gt 500)

The following method is dangerous: it shows how to delete all documents in the current collection:

// removes all documents
def deleteAllObjectsFromCollection(coll: MongoCollection) {
    coll.remove(MongoDBObject.newBuilder.result)
}

(Be careful with that one.)

Discussion

If you’ve been following along with the MongoDB recipes in this chapter, you can experiment with these approaches by saving the following code to a file named DeleteApple.scala in the root directory of your SBT project:

import com.mongodb.casbah.Imports._

object DeleteApple extends App {
    var collection = MongoFactory.collection

    // delete AAPL
    val query = MongoDBObject("symbol" -> "AAPL")
    val result = collection.findAndRemove(query)
    println("result: " + result)
}

You can also clone my complete Scala + Casbah + MongoDB project from GitHub.

If your database has a document whose symbol field is AAPL, when you run this object with sbt run, the result will show the document that was deleted:

result: Some({ "_id" : { "$oid" : "5026b22c300478e85a145d43"}, "symbol" : "AAPL" , "price" : 600.0})

The following complete code shows how to delete multiple documents:

import com.mongodb.casbah.Imports._

object DeleteMultiple extends App {
    var collection = MongoFactory.collection
    // delete all documents with price > 200
    collection.remove("price" $gt 200)
}

In this case, the remove method doesn’t return anything interesting, so I don’t assign it to a result.

See Also

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.