mongo-scala-driver中无法更新选项[X]字段

时间:2018-12-03 05:46:19

标签: mongo-scala-driver

我有这个案例课:

case class CodeRegister(id: String, name: String, code: Option[CodeData])
case class CodeData(code: String, expiry: Long)

我已经通过CodeRegister方法注册了CodeDatafromProviders。现在,我尝试使用此代码更新字段code

collection.updateOne(equal("id",record. id),
    set("code", record.code)).
    toFutureOption()

但是,我遇到了这个错误

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class scala.Some.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:37)
at com.mongodb.client.model.Updates$SimpleUpdate.toBsonDocument(Updates.java:445)
at com.mongodb.client.model.Updates$CompositeUpdate.toBsonDocument(Updates.java:614)
at com.mongodb.internal.operation.Operations.toBsonDocument(Operations.java:489)
at com.mongodb.internal.operation.Operations.bulkWrite(Operations.java:369)
at com.mongodb.internal.operation.Operations.updateOne(Operations.java:318)
at com.mongodb.internal.operation.AsyncOperations.updateOne(AsyncOperations.java:168)
at com.mongodb.async.client.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:702)
at com.mongodb.async.client.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:659)
at com.mongodb.async.client.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:653)
at org.mongodb.scala.MongoCollection.$anonfun$updateOne$1(MongoCollection.scala:861)
at org.mongodb.scala.MongoCollection.$anonfun$updateOne$1$adapted(MongoCollection.scala:861)
at org.mongodb.scala.internal.ObservableHelper$$anon$1.apply(ObservableHelper.scala:36)
at org.mongodb.scala.internal.ObservableHelper$$anon$1.apply(ObservableHelper.scala:35)
at com.mongodb.async.client.SingleResultCallbackSubscription.requestInitialData(SingleResultCallbackSubscription.java:38)
at com.mongodb.async.client.AbstractSubscription.tryRequestInitialData(AbstractSubscription.java:151)
at com.mongodb.async.client.AbstractSubscription.request(AbstractSubscription.java:84)
at org.mongodb.scala.ObservableImplicits$BoxedSubscription.request(ObservableImplicits.scala:490)
at org.mongodb.scala.ObservableImplicits$ScalaObservable$$anon$2.onSubscribe(ObservableImplicits.scala:366)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable$$anon$3.onSubscribe(ObservableImplicits.scala:441)
at org.mongodb.scala.internal.SubscriptionCheckingObserver.onSubscribe(SubscriptionCheckingObserver.scala:28)
at org.mongodb.scala.Observer.onSubscribe(Observer.scala:85)
at org.mongodb.scala.Observer.onSubscribe$(Observer.scala:85)
at org.mongodb.scala.internal.SubscriptionCheckingObserver.onSubscribe(SubscriptionCheckingObserver.scala:21)
at com.mongodb.async.client.SingleResultCallbackSubscription.<init>(SingleResultCallbackSubscription.java:33)
at com.mongodb.async.client.Observables$2.subscribe(Observables.java:78)
at org.mongodb.scala.ObservableImplicits$BoxedObservable.subscribe(ObservableImplicits.scala:474)
at org.mongodb.scala.ObservableImplicits$ToSingleObservable.subscribe(ObservableImplicits.scala:433)
at org.mongodb.scala.ObservableImplicits$ScalaObservable.headOption(ObservableImplicits.scala:358)
at org.mongodb.scala.ObservableImplicits$ScalaSingleObservable.toFutureOption(ObservableImplicits.scala:419)

我通过添加SomeCodec并在CodeData上添加了新的匹配条件,来尝试了this link中的解决方案,

case Some(v: CodeData) => {
  writer.writeStartDocument()
  writer.writeString(v.code)
  writer.writeInt64(v.expire)
  writer.writeEndDocument()
}

但是我遇到了这个错误

org.bson.BsonInvalidOperationException: writeString can only be called when State is VALUE, not when State is NAME

现在,我不知道下一步该怎么做。任何帮助表示赞赏。

0 个答案:

没有答案