【问题标题】:MongoDB Scala driver. How to update only changed document's fields from the case class objectMongoDB Scala 驱动程序。如何仅更新案例类对象中更改的文档字段
【发布时间】:2021-02-09 05:38:55
【问题描述】:

我有以下案例类:

  @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 请求给出

【问题讨论】:

    标签: mongodb scala graphql caliban


    【解决方案1】:

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

    val firstJson =  parse(userConfigFromRequest.toBsonDocument(classOf[UserConfig], codecRegistry).toJson).extract[Map[String, Any]]
    val secondJson = parse(config.toBsonDocument(classOf[UserConfig], codecRegistry).toJson).extract[Map[String, Any]]
    val merged = deepMerge(firstJson, secondJson)
    
      def deepMerge(
                     map1: Map[String, Any],
                     map2: Map[String, Any],
                     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
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多