Skip to content

Instantly share code, notes, and snippets.

@terjokhin
Created March 31, 2017 09:33
Show Gist options
  • Save terjokhin/54357dc5c12a2299f676f44ea5885a9f to your computer and use it in GitHub Desktop.
Save terjokhin/54357dc5c12a2299f676f44ea5885a9f to your computer and use it in GitHub Desktop.
object MongoApp extends App {
case class Data(_id: ObjectId, data: String)
object Data {
def apply(data: String): Data = new Data(new ObjectId(), data)
}
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
import org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson.codecs.Macros._
val codecRegistry = fromRegistries(fromProviders(classOf[Data]), DEFAULT_CODEC_REGISTRY)
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("test_007").withCodecRegistry(codecRegistry)
val collection: MongoCollection[Data] = database.getCollection("data")
val data1 = Data("Data1")
val data2 = Data("Data2")
Await.result(collection.insertOne(data1).head(), 5 seconds)
Await.result(collection.insertOne(data2).head(), 5 seconds)
val results = Await.result(collection.find().toFuture(), 10 seconds)
println(results)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment