As Casbah wraps the Java driver, so querying against MongoDB is essentially the same. The following methods for finding data:
findReturns a cursor
findOneReturns an Option - either Some(MongoDBObject) or None
findByIdReturns an Option - either Some(MongoDBObject) or None
findAndModifyFinds the first document in the query, updates and returns it.
findAndRemoveFinds the first document in the query, removes and returns it.
The following methods insert and update data:
saveSaves an object to the collection
insertSaves one or more documents to the collection
updateUpdates any matching documents operation
For more information about create, read, update and delete (CRUD) operations in MongoDB see the core operations documentation.
The collection API documentation has a full list of methods and their signatures for interacting with collections.
Mongodb 2.6 introduces operations. The bulk operations builder can be used to construct a list of write operations to perform in bulk for a single collection. Bulk operations come in two main flavors.
- Ordered bulk operations. These operations execute all the operation in order and error out on the first write error.
- Unordered bulk operations. These operations execute all the operations in parallel and aggregates up all the errors. Unordered bulk operations do not guarantee order of execution.
An example of using the bulk api:
val collection = MongoClient()("test")("bulkOperation") collection.drop() val builder = collection.initializeOrderedBulkOperation builder.insert(MongoDBObject("_id" -> 1)) builder.insert(MongoDBObject("_id" -> 2)) builder.insert(MongoDBObject("_id" -> 3)) builder.find(MongoDBObject("_id" -> 1)).updateOne($set("x" -> 2)) builder.find(MongoDBObject("_id" -> 2)).removeOne() builder.find(MongoDBObject("_id" -> 3)).replaceOne(MongoDBObject("_id" -> 3, "x" -> 4)) val result = builder.execute()
For more information see the bulk operations documentation.
MongoDB queries work by providing a document to match against. The
simplest query object is an empty one eg:
matches every record in the database.
MongoDBObject is a simple Map-like class, that wraps the Java driver DBObject and provides some nice Scala interfaces:
val query = MongoDBObject("foo" -> "bar") ++ ("baz" -> "qux")
DBObjects have a builder and as such you can also build MongoDBObjects that way:
val builder = MongoDBObject.newBuilder builder += "foo" -> "bar" builder += "baz" -> "qux" val query = builder.result
Remember to import casbah: