How to perform 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 it's hard for me to follow the tutorial as I am still learning Scala.

All I wanted to learn was how to perform simple CRUD operations using Casbah, to get started with before I can move on.

The following are 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 which Kasbakh code should perform these tasks?

(1) save as hotels in mongodb

(2) find all hotels with stars equal to 4 or greater than 4. This should give me a list over which I can repeat

(3) find a hotel called “Super Nice” and change its name to “Ultra Nice”

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

+6
source share
1 answer

Here you can see how to embed data: Casbah wiki

If you want to directly save class classes (without using MongoDBObject) in MongoDB, you should take a look at Salat and SalatDao: Salat presentation

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

0
source

Source: https://habr.com/ru/post/891453/


All Articles