简体   繁体   中英

How to do CRUD operations on domain models using Casbah for MongoDb?

There is a tutorial on Casbah:

http://api.mongodb.org/scala/casbah/current/tutorial.html

But I find it hard to follow the tutorial as I am still learning Scala.

All I wanted to find out how to do simple CRUD operations using Casbah to begin with before I can go more advanced.

Given below domain models:

class Hotel (var name: String, var stars: Int, val address:  Address)

class Address(var street:String, var city: String, var postCode: String)



val address = new Address(street = "1234 st", city = "edmond", postCode = "1232234", country = "USA" )


  val hotel = new Hotel(name = "Super Nice", stars = 4, address =  address)

val address2 = new Address(street = "main st", city = "edmond", postCode = "1232234", country = "USA" )

val hotel2 = new Hotel(name = "Big Hotel", stars = 4, address =  address2)

Given above what Casbah code is to achieve these tasks?

(1) save both hotels in mongodb

(2) find all hotels that have stars equal to 4 or greater than 4. this should give me a list over which I can iterate

(3) find a hotel by the name "Super Nice" and change its name to "Ultra Nice"

(4) get addresses of all hotels and change country to lower case and save in database

Here you can see how to insert data: Casbah wiki

If you want to directly save case classes (without needing a MongoDBObject) in MongoDB you should have a look at Salat and SalatDao: Salat presentation

In my opinion, the answers to question (2) - (4) can be found easily in the documentation of casbah and salat.

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