2

Can not find any description in Casbah tutorial (http://mongodb.github.com/casbah/tutorial.html) how to write / update / delete objects in MongoDB. Please, help with examples or tell what classes to look for these methods. Thanks!

Anton Ashanin
  • 1,817
  • 5
  • 30
  • 43
  • The tutorial must have changed, since now it seems to have all the stuff I'd need on write/update/delete objects. Another tutorial (http://api.mongodb.org/scala/casbah/2.0/tutorial.html) was lacking these - reading both should have all the info one needs. Or did I miss something? – akauppi Dec 23 '13 at 20:02
  • You are correct. I added a note in the answer below to mention that the documentation was updated a few months after the question was asked and answered to include a section on CRUD operations. http://mongodb.github.io/casbah/tutorial.html#doing-crud-operations – Dave Swartz Feb 19 '14 at 04:42

2 Answers2

4

Updated Answer

A few months after this question was asked and answered the Casbah documentation was updated to include the Doing CRUD operations section. Perhaps this question/answer helped make that happen. Or perhaps someone realized not documenting CRUD operations in a DB tutorial was cray cray.

Original Answer

In order to understand why the Cashbah tutorial does not provide examples of how to insert, update or remove documents from a MongoDB database, a good place to start is the first paragraph of the Casbah Documentation:

Casbah is a Scala toolkit for MongoDB---We use the term "toolkit" rather than "driver", as Casbah integrates a layer on top of the official mongo-java-driver for better integration with Scala. This is as opposed to a native implementation of the MongoDB wire protocol, which the Java driver does exceptionally well. Rather than a complete rewrite, Casbah uses implicits, and Pimp My Library code to enhance the existing Java code.

Casbah is a toolkit to augment the Java driver. Therefore one must first read the Java Driver Documentation and then the Casbah Documentation in order to effectively use Casbah.

To further motivate the suggestion, take a look at how Casbah wraps some methods from the Java Driver:

trait MongoCollectionBase extends Logging { self =>
  ...
  val underlying: DBCollection
  ...
  def save[A <% DBObject](jo: A) = underlying.save(jo)
  ...
  def update[A <% DBObject, B <% DBObject](q: A, o: B) = underlying.update(q, o)
  ...
  def remove[A <% DBObject](o: A) = underlying.remove(o)
  ...

As the above excerpt demonstrates, Casbah's MongoCollection is a proxy for the Java Driver's DBCollection. This is meant to illustrate that if something is not being handled by the Casbah toolkit it is being handled by a call to the Java Driver.

Below are links to examples of how to insert, update and remove documents from a MongoDB database using the Java driver:

  • How to insert and update documents in the database is documented here;
  • An example of how to remove a document is available here.

It should be straightforward to port these examples to use Casbah now that you understand how Casbah relates to the Java Driver.

Dave Swartz
  • 910
  • 6
  • 14
1

I prepared some examples of how to use casbah, very simple so far. I'll add more functionality later.

https://github.com/talgendler/casbah

object AddressMongoConverter {
  def convertToMongoObject(address: Address): DBObject = {
    MongoDBObject(
      STREET -> address.street,
      ZIP_CODE -> address.zipCode,
      CITY -> address.city,
      COUNTRY -> address.country
    )
  }

  def convertFromMongoObject(db: DBObject): Address = {
    Address(
      street = db.getAsOrElse[String](STREET, mongoFail),
      zipCode = db.getAsOrElse[Int](ZIP_CODE, mongoFail),
      city = db.getAsOrElse[String](CITY, "Tel-Aviv"), // slightly different get
      country = db.getAsOrElse[String](COUNTRY, "Israel")
    )
  }
}
Tal G.
  • 483
  • 5
  • 15
  • hmm.. your sample here does not involve the write/update/delete that the question author asked for - but it covers data conversion (the stuff behind the link might, didn't see there). – akauppi Dec 23 '13 at 19:19
  • @akauppi The stuff behind the link have :) – Tal G. Dec 24 '13 at 11:33