simple-mongo

A library for easy usage of the mongo-scala-driver.

Features:

Additional Documentation

Dependency Setup

Support Scala 2.12 and Scala 2.13.

sbt
libraryDependencies += "com.sfxcode.nosql" %% "simple-mongo" % "2.0.1"
Maven
<dependency>
  <groupId>com.sfxcode.nosql</groupId>
  <artifactId>simple-mongo_2.13</artifactId>
  <version>2.0.1</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.sfxcode.nosql', name: 'simple-mongo_2.13', version: '2.0.1'
}

Licence

Apache 2

Example: Model

Create custom model classes.


case class Student(_id: Long, name: String, scores: List[Score], image: Option[ObjectId]) case class Score(score: Double, `type`: String) case class Grade(_id: ObjectId, student_id: Long, class_id: Long, scores: List[Score])

Example: Database

package com.sfxcode.nosql.mongo.test

import com.sfxcode.nosql.mongo.database.DatabaseProvider
import com.sfxcode.nosql.mongo.model._
import com.sfxcode.nosql.mongo.server.LocalServer
import com.sfxcode.nosql.mongo.{GridFSDAO, MongoDAO}
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
import org.bson.codecs.configuration.CodecRegistry
import org.mongodb.scala.bson.codecs.Macros._

object UniversityDatabase {
  // create local test server (mongo-java-server)
  val LocalTestServer = LocalServer.fromPath("unit.test.local.mongo.server")

  // create codecs for custom classes
  private val universityRegistry: CodecRegistry = fromProviders(classOf[Student], classOf[Score], classOf[Grade])

  private val registry: CodecRegistry = fromRegistries(universityRegistry)

  // create provider
  val provider: DatabaseProvider = DatabaseProvider.fromPath(configPath = "unit.test.mongo.local", registry = registry)

  // setup DAO objects with mongo collection names

  object StudentDAO extends MongoDAO[Student](provider, "university-students")

  object GradeDAO extends MongoDAO[Book](provider, "university-grades")

  object SudentImagesDAO extends GridFSDAO(provider, "university-images")

}