【问题标题】:Reading document array to a map from mongodb scala driver 2.4.2从 mongodb scala 驱动程序 2.4.2 读取文档数组到地图
【发布时间】:2020-03-20 21:43:31
【问题描述】:

我刚刚从 casbah 切换到 mongodb scala 驱动程序 2.4.2,这里的一切都有些不同[异步编程],我在这里寻找的只是从 mongodb 文档中读取数组并将这个数组转换为地图。

谁能帮我完成一些缺少的步骤。

示例文档:

{
 "_id":"ESB0500_CROSSREF_TEST",
 "country_code":"WW",
 "instance_id":"1",
 "values": 
 {"1":"A","2":"B","3":"C","4":"D","5":"E","6":"F","7":"G","8":"H","9":"I","10":"J","11":"K","12":"L","13":"M","14":"N","15":"O","16":"P","17":"Q","18":"R","19":"S","20":"T","21":"U","22":"V","23":"W","24":"X","25":"Y","26":"Z"}
}

在这里,我希望将值数组转换为映射,其中键是数字,值是字母,如键 1 和值 A

到目前为止我的查询

val result = collection_obj.find(
      and(
        equal("_id",id),
        equal("country_code",country_code),
        equal("instance_id",instance_id)
      )
    ).projection(
      fields(
        include("values"),
        excludeId()
      )
    )

【问题讨论】:

    标签: mongodb scala


    【解决方案1】:

    如果您正在寻找的只是将您的特殊数组转换为一种地图,那么这可能会对您有所帮助。

    val array = Array("1", "A", "2", "B", "3", "C")
    
    array.grouped(2).map { couple =>
      couple(0).toInt -> couple(1)
    }.toMap
    

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多