Search code examples
mongodbscalachangestream

mongodb change stream cannot resolve subscribe


i want to set a change stream on mongo db, But there is a "Cannot resolve overloaded method subscribe" and I'm not able to find the source of the problem :

import org.mongodb.scala.{Document, MongoClient, MongoCollection, MongoDatabase}

object App {

  def main(args: Array[String]): Unit = {

      val mongoClient: MongoClient = MongoClient("mongodb://localhost")

      val database: MongoDatabase = mongoClient.getDatabase("db")

      val collection: MongoCollection[Document] = database.getCollection("col")
          collection.watch().subscribe(
            (doc: Document) => println(doc.toJson),
            (t: Throwable) => println(""),
              () => println()
          )
  }

}


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

Solution

  • replace:

    (doc: Document) => println(doc.toJson),
    

    with:

    (csd: ChangeStreamDocument[Document]) => println(csd.getFullDocument()),