简体   繁体   中英

Scala: Example of using Casbah to write / update / delete objects in MongoDB?

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!

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.

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")
    )
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM