简体   繁体   English

Ma.net pattern for Scala MongoDB驱动程序

[英]Magnet pattern for Scala MongoDB driver

The documentation describes using the ma.net pattern to get implicit conversion to BSON types.该文档描述了使用 ma.net 模式来隐式转换为 BSON 类型。 See on this page http://mongodb.github.io/mongo-java-driver/4.1/driver-scala/bson/scala-documents/ .请参阅本页http://mongodb.github.io/mongo-java-driver/4.1/driver-scala/bson/scala-documents/ I have tried defining an implicit object that extends BsonTransformer, but it fails to find a codec for the type.我尝试定义一个隐式的 object 来扩展 BsonTransformer,但它找不到该类型的编解码器。 Am I missing something / has someone got this working?我错过了什么/有人让这个工作了吗? Sample code below, assume the insert method is being called.下面的示例代码,假设正在调用插入方法。

case class CustomType(specialString: String)

implicit object TransformCustomType extends BsonTransformer[CustomType] {

  def apply(value: CustomType): BsonString = 
      BsonString(value.specialString)
}

lazy val db: MongoDatabase = client.getDatabase(dbName).withCodecRegistry(DEFAULT_CODEC_REGISTRY)
lazy val testCollection: MongoCollection[CustomType] = db.getCollection[CustomType](collectionName)

def insert: Future[Completed] = testCollection.insertOne(CustomType("a")).toFuture

Error - org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.bla.BlaClass$CustomType.错误 - org.bson.codecs.configuration.CodecConfigurationException:找不到 class com.bla.BlaClass$CustomType 的编解码器。

*note that I am aware this can be done with *请注意,我知道这可以通过

val codecRegistry = fromRegistries(fromProviders(classOf[CustomType]))

but I am just using this example to ask to learn the ma.net pattern for a messier case.但我只是用这个例子来要求学习 ma.net 模式以应对更复杂的情况。

If there is an implicit BsonTransformer[T] (instance of type class BsonTransformer ) in a scope then there are implicit conversions如果 scope 中存在隐式BsonTransformer[T]类型为 class BsonTransformer的实例),则存在隐式转换

  • T => CanBeBsonValue ( CanBeBsonValue is a wrapper over BsonValue ), T => CanBeBsonValueCanBeBsonValueBsonValue的包装器),

  • (String, T) => CanBeBsonElement ( CanBeBsonElement is a wrapper over BsonElement , which is a "tuple" of String and BsonValue ), (String, T) => CanBeBsonElementCanBeBsonElementBsonElement的包装器,它是StringBsonValue的“元组”),

  • Iterable[(String, T)] => CanBeBsonElements ( CanBeBsonElements is a wrapper over Iterable[BsonElement] ) Iterable[(String, T)] => CanBeBsonElementsCanBeBsonElementsIterable[BsonElement]的包装器)

defined in BsonMa.nets ( CanBeBsonValue , CanBeBsonElement , CanBeBsonElements are ma.nets 1 2 ).BsonMa.nets中定义( CanBeBsonValueCanBeBsonElementCanBeBsonElements是 ma.nets 1 2 )。

Then Document can be created via factory methods然后可以通过工厂方法创建Document

def apply(elems: CanBeBsonElement*): Document

def apply(elem: CanBeBsonElements): Document

So try所以试试

val doc = Document("a" -> CustomType("bb"))

val testCollection: MongoCollection[Document] = ???

testCollection.insertOne(doc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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