simple-mongo


License

License

GroupId

GroupId

com.sfxcode.nosql
ArtifactId

ArtifactId

simple-mongo_2.11
Last Version

Last Version

1.6.2
Release Date

Release Date

Type

Type

jar
Description

Description

simple-mongo
simple-mongo
Project URL

Project URL

https://github.com/sfxcode/simple-mongo
Project Organization

Project Organization

com.sfxcode.nosql
Source Code Management

Source Code Management

https://github.com/sfxcode/simple-mongo

Download simple-mongo_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.sfxcode.nosql/simple-mongo_2.11/ -->
<dependency>
    <groupId>com.sfxcode.nosql</groupId>
    <artifactId>simple-mongo_2.11</artifactId>
    <version>1.6.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.sfxcode.nosql/simple-mongo_2.11/
implementation 'com.sfxcode.nosql:simple-mongo_2.11:1.6.2'
// https://jarcasting.com/artifacts/com.sfxcode.nosql/simple-mongo_2.11/
implementation ("com.sfxcode.nosql:simple-mongo_2.11:1.6.2")
'com.sfxcode.nosql:simple-mongo_2.11:jar:1.6.2'
<dependency org="com.sfxcode.nosql" name="simple-mongo_2.11" rev="1.6.2">
  <artifact name="simple-mongo_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.sfxcode.nosql', module='simple-mongo_2.11', version='1.6.2')
)
libraryDependencies += "com.sfxcode.nosql" % "simple-mongo_2.11" % "1.6.2"
[com.sfxcode.nosql/simple-mongo_2.11 "1.6.2"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.mongodb.scala : mongo-scala-driver_2.11 jar 2.6.0
com.typesafe.scala-logging : scala-logging_2.11 jar 3.9.0

test (6)

Group / Artifact Type Version
org.specs2 : specs2-core_2.11 jar 4.4.1
ch.qos.logback : logback-classic jar 1.2.3
com.typesafe : config jar 1.3.3
joda-time : joda-time jar 2.10.1
org.json4s : json4s-native_2.11 jar 3.6.4
com.github.pathikrit : better-files_2.11 jar 3.7.0

Project Modules

There are no modules declared in this project.

simple-mongo

A library for easy usage of the mongo-scala-driver (4.x). Full MongoDB Functionality in Scala with a few lines of code.

MongoDB Support

Support MongoDB 2.6 to 4.2.

Features

  • Easy Database setup with Config file
  • Compressor Support
  • Database Commands, Changes Stream, ...
  • DAO Pattern for collection
  • DAO Pattern for GridFS
  • GridFS: Upload from InputStream, Download to OutputStream
  • Implicit conversions for Document, Bson, ObjectID ...
  • Reactive Streams Support
  • Json loading from File, Conversion to plain Json
  • Local Server Support mongo-java-server
  • Collection Sync
  • ...

Documentation

Documentation can be found here.

Version

Scala Version is 2.13.x / 2.12.x.

Travis

Build Status

Download

Download

Licence

Apache 2 License.

Usage

Add following lines to your build.sbt (replace x.x with the actual Version)


libraryDependencies += "com.sfxcode.nosql" %% "simple-mongo" % "2.x.x"

Define MongoDB Connection and DAO objects for automatic case class conversion.

import java.util.Date

import com.sfxcode.nosql.mongo.MongoDAO
import com.sfxcode.nosql.mongo.database.DatabaseProvider
import org.bson.codecs.configuration.CodecRegistries._
import org.mongodb.scala.bson.ObjectId
import org.mongodb.scala.bson.codecs.Macros._

/**
 * import mongodb restaurants sample data
 */
object RestaurantDatabase {

  case class Address(street: String, building: String, zipcode: String, coord: List[Double])

  case class Grade(date: Date, grade: String, score: Int)

  case class Restaurant(restaurant_id: String, name: String, borough: String, cuisine: String,
    grades: List[Grade], address: Address, _id: ObjectId = new ObjectId())

  private val registry = fromProviders(classOf[Restaurant], classOf[Address], classOf[Grade])

  val provider = DatabaseProvider("test", registry)

  object RestaurantDAO extends MongoDAO[Restaurant](provider, "restaurants")

}

Import the database object and execute some find and CRUD functions on the DAO object ...

import com.sfxcode.nosql.mongo.demo.restaurant.RestaurantDemoDatabase._
import com.sfxcode.nosql.mongo._

trait RestaurantDemoDatabaseFunctions {

  /**
   * single result with implicit conversion to Entity Option
   */
  def findRestaurantByName(name: String): Option[Restaurant] =
    RestaurantDAO.find("name", name)

  def restaurantsSize: Long = RestaurantDAO.count()

  /**
   * result with implicit conversion to List of Entities
   */
  def findAllRestaurants(filterValues: Map[String, Any] = Map()): List[Restaurant] =
    RestaurantDAO.find(filterValues)

Use the mongodb functions in your app ...

 object RestaurantDemoApp extends App with RestaurantDemoDatabaseFunctions {
 
   // find specific restaurant by name as Option Result
   val restaurant = findRestaurantByName("Dj Reynolds Pub And Restaurant")
 
   println(restaurant)
 
   // use count function
   println(restaurantsSize)
 
   // find restaurants by filter
   private val filter = Map("address.zipcode" -> "10075", "cuisine" -> "Italian")
   val restaurants = findAllRestaurants(filter)
 
   restaurants.sortBy(r => r.name).foreach(r => println(r.name))
 
 }

Write some spec tests ...

import com.sfxcode.nosql.mongo.demo.restaurant.RestaurantDemoDatabase._
import org.specs2.mutable.Specification

class RestaurantDemoSpec extends Specification with RestaurantDemoDatabaseFunctions {

  "RestaurantDemo" should {

    "find restaurant by name in" in {

      val restaurantSearch = findRestaurantByName("Dj Reynolds Pub And Restaurant")
      restaurantSearch must beSome[Restaurant]
      val restaurant = restaurantSearch.get
      restaurant.borough must be equalTo "Manhattan"
    }
  }

}

Supporters

JetBrains is supporting this open source project with:

Intellij IDEA

Versions

Version
1.6.2
1.6.0
1.5.6
1.5.5
1.5.4
1.5.3