简体   繁体   中英

Scala mongodb driver

Insertion is working fine. Problem is with find()

For find(),

val collection: MongoCollection[Machine] =    mongoDB.getCollection(DbConstants.COLLECTION_NAME_MACHINE)

collection.find().subscribe(new Observer[Machine] {

  override def onNext(result: Machine): Unit = println(s"Machine is $result")

  override def onError(e: Throwable): Unit = e.printStackTrace()

  override def onComplete(): Unit = println("Completed")
})

It shows Error:

org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.

In data model for Machine is:

case class Machine(_id: String,
    @BsonProperty(DbConstants.FIELD_SITE_ID)siteId: BsonObjectId)

folllowing tutorial from: http://mongodb.github.io/mongo-scala-driver/2.2/getting-started/quick-tour-case-classes/

You may have null values in the data and that is causing the issue.

Exception when geting distinct values of a field from Mongo collection using mongo Java Driver 3.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM