如何使用 Casbah for MongoDb 对域模型进行 CRUD 操作?
有一个关于Casbah的教程:
http://api.mongodb.org/scala/ casbah/current/tutorial.html
但我发现很难遵循该教程,因为我仍在学习 Scala。
我只想了解如何使用 Casbah 进行简单的 CRUD 操作 在我可以更进一步之前。
给出下面的域模型:
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)
上面给出的 Casbah 代码是用来实现这些任务的?
(1) 将两家酒店保存在 mongodb 中
(2) 找到所有星级等于 4 或大于 4 的酒店。这应该给我 我可以迭代的列表
(3)找到一家名为“Super Nice”的酒店并将其名称更改为“Ultra Nice”
(4)获取所有酒店的地址并将国家/地区更改为小写并保存在数据库中
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里您可以看到如何插入数据: Casbah wiki
如果你想直接在 MongoDB 中保存案例类(不需要 MongoDBObject),你应该看看 Salat 和 SalatDao:Salat 演示
在我看来,问题(2)-(4)的答案可以在 casbah 和 salat 的文档中轻松找到。
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.