MongoDB Scala 驱动程序如何仅更新案例类对象中更改的文档字段

问题描述

我有以下案例类:

  @GQLDirective(federation.Key("User Config"))
case class UserConfig(
                        userId: Int,personalGroup: Option[PersonalGroup] = None,accountGroup: Option[AccountGroup] = None
                       ) extends Bson {
    override def toBsonDocument[TDocument](documentClass: Class[TDocument],codecRegistry: CodecRegistry): BsonDocument = {
      new BsonDocumentWrapper[UserConfig](this,codecRegistry.get(classOf[UserConfig]))
    }
  }

并尝试以这种方式更新已存储的文档:

def update(userConfig: UserConfig) = {
    collection.updateOne(equal("userId",1),userConfig).headOption()
  }

但是当我尝试这样做时,出现以下错误: Invalid BSON field name userId

我也尝试过使用 replaceOne 方法,但它会替换整个对象并删除我不想删除的字段。

我想要达到的目标: 我只想在 mongodb 文档中保存更改的字段。这些字段由 graphql 请求提供

解决方法

现在,我找到了一种方法来转换和递归合并新对象和旧对象,就像这样。如果有人有更好的代码或意见 - 我很乐意聊天

val firstJson =  parse(userConfigFromRequest.toBsonDocument(classOf[UserConfig],codecRegistry).toJson).extract[Map[String,Any]]
val secondJson = parse(config.toBsonDocument(classOf[UserConfig],Any]]
val merged = deepMerge(firstJson,secondJson)

  def deepMerge(
                 map1: Map[String,Any],map2: Map[String,accum: MutableMap[String,Any] = MutableMap[String,Any]()
               ): MutableMap[String,Any] = {
    map1 foreach {
      case (key,value: Map[String,Any]) => {
        map2.get(key) match {
          case Some(oldValue: Map[String,Any]) => {
            accum.put(key,deepMerge(value,map2,MutableMap(oldValue.toSeq: _*) ))
          }
          case None => accum.put(key,value)
        }
      }
      case (key,value) => {
        accum.put(key,value)
      }
    }
    accum
  }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...