简体   繁体   English

如何使用Casbah for MongoDb对域模型进行CRUD操作?

[英]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 http://api.mongodb.org/scala/casbah/current/tutorial.html

But I find it hard to follow the tutorial as I am still learning Scala. 但我发现很难按照教程,因为我还在学习Scala。

All I wanted to find out how to do simple CRUD operations using Casbah to begin with before I can go more advanced. 所有我想知道如何使用Casbah进行简单的CRUD操作,然后再开始更先进。

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? 鉴于上面的Casbah代码是什么来实现这些任务?

(1) save both hotels in mongodb (1)保存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 (2)找到星星等于4或大于4的所有酒店。这应该给我一个我可以迭代的列表

(3) find a hotel by the name "Super Nice" and change its name to "Ultra Nice" (3)找到一家名为“Super Nice”的酒店,并将其名称改为“Ultra Nice”

(4) get addresses of all hotels and change country to lower case and save in database (4)获取所有酒店的地址并将国家/地区更改为小写并保存在数据库中

Here you can see how to insert data: Casbah wiki 在这里您可以看到如何插入数据: 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 如果你想在MongoDB中直接保存案例类(不需要MongoDBObject),你应该看看Salat和SalatDao: Salat演示文稿

In my opinion, the answers to question (2) - (4) can be found easily in the documentation of casbah and salat. 在我看来,问题(2) - (4)的答案可以在casbah和salat的文档中轻松找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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