0

插入工作正常。问题在于 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")
})

它显示错误:

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

机器的数据模型是:

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

以下教程来自: http ://mongodb.github.io/mongo-scala-driver/2.2/getting-started/quick-tour-case-classes/

4

1 回答 1

0

您可能在数据中有空值,这会导致问题。

使用 mongo Java Driver 3.0 从 Mongo 集合中获取字段的不同值时出现异常

于 2018-05-03T12:16:02.543 回答