@@ -25,44 +25,58 @@ Define MongoDB Connection and DAO objects for automatic case class conversion.
2525
2626
2727``` scala
28- import com .sfxcode .nosql .mongo .json4s .DefaultBsonSerializer ._
29- import org .mongodb .scala ._
3028
31- import com . sfxcode . nosql . mongo . model . _
29+ import java . util . Date
3230
33- object Database {
31+ import com .sfxcode .nosql .mongo .MongoDAO
32+ import com .sfxcode .nosql .mongo .database .DatabaseProvider
33+ import org .bson .codecs .configuration .CodecRegistries ._
34+ import org .mongodb .scala .bson .ObjectId
35+ import org .mongodb .scala .bson .codecs .Macros ._
3436
35- val mongoClient : MongoClient = MongoClient ()
37+ /**
38+ * import mongodb restaurants sample data
39+ */
40+ object RestaurantDatabase {
3641
37- val database : MongoDatabase = mongoClient.getDatabase( " simple_mongo_test " )
42+ case class Address ( street : String , building : String , zipcode : String , coord : List [ Double ] )
3843
39- val bookCollection : MongoCollection [ Document ] = database.getCollection( " books " )
44+ case class Grade ( date : Date , grade : String , score : Int )
4045
41- object BookDAO extends MongoDAO [Book ](Database .bookCollection)
42- }
43- ```
46+ case class Restaurant (restaurant_id : String , name : String , borough : String , cuisine : String ,
47+ grades : List [Grade ], address : Address , _id : ObjectId = new ObjectId ())
4448
49+ private val registry = fromProviders(classOf [Restaurant ], classOf [Address ], classOf [Grade ])
4550
46- Import the database object and execute find and CRUD functions on the DAO object.
51+ val database = DatabaseProvider ( " test " , registry)
4752
48- ``` scala
53+ object RestaurantDAO extends MongoDAO [ Restaurant ](database, " restaurants " )
4954
50- import Database . _
55+ }
5156
52- case class Book (id : Option [Int ], title : String , pages : BigInt , author : Author ,
53- set : Set [Long ] = Set (1 , 2 , 3 ), released : Boolean = true , releaseDate : Date = new Date (),
54- _id : ObjectId = new ObjectId ())
57+
58+ ```
5559
5660
57- val books : List [ Book ] = BookDAO .findAll()
61+ Import the database object and execute find and CRUD functions on the DAO object.
5862
63+ ``` scala
64+ import RestaurantDatabase ._
65+
66+ import com .sfxcode .nosql .mongo ._
67+
68+ object RestaurantApp extends App {
69+
70+ val restaurant : Option [Restaurant ] = RestaurantDAO .find(" name" , " Dj Reynolds Pub And Restaurant" )
71+
72+ println(restaurant.get.grades)
73+
74+ val restaurants : List [Restaurant ] = RestaurantDAO .find(Map (" address.zipcode" -> " 10075" , " cuisine" -> " Italian" ))
75+
76+ restaurants.sortBy(r => r.name).foreach(r => println(r.name))
77+
78+ }
5979
60- val scalaBook = Book (Some (1 ), " Programming In Scala" , 852 , Author (" Martin Odersky" ))
61-
62- BookDAO .insertResult(scalaBook)
63-
64- BookDAO .deleteResult(scalaBook)
65-
6680```
6781
6882
0 commit comments