1

I'm using Scala, Mongo DB, and the mongo-scala driver simple example to insert data into Mongo.

libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "2.9.0"

// step 1 : connect to mongo 
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017")

// step 2 : connect to db
val database: MongoDatabase = mongoClient.getDatabase("mydb")

// step 3 : get the collection 
val collection: MongoCollection[Document] = database.getCollection("test")

// step 4 : create document 
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database", "count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))

//step 5 : insert in to db 
collection.insertOne(doc).results();

Here in the last step the method results() is not found, but they gave it in the documentation.

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54

1 Answers1

2

From the documentation:

Helpers used in the Quick Tour

For the Quick Tour we use custom implicit helpers defined in Helpers.scala. These helpers get and print results and although this is an artificial scenario for asynchronous code we block on the results of one example before starting the next, so as to ensure the state of the database. The Helpers object provides the following methods:

  • results()

    Blocks until the Observable is completed and returns the collected results

...

Look at the source code for the Quick Tour, and you'll see the following import:

import tour.Helpers._
Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54