如何通过casbah获取文档的key?

发布于 2024-11-09 22:30:02 字数 521 浏览 0 评论 0原文

我的文档如下所示:

{
  "dynamic_field" : "...",
  "another_dynamic_field" : "..."
  "yet_another_dynamic_field" : "..."
}

“动态字段”意味着我不知道它的名称。所以我想获取包含该文档的键的字符串集合。然后通过键获取文档的值(值的结构已明确定义)。

所以,我尝试做以下

val dbObject = ... 
val keys = dbObject.keys()
for(
  key <- keys; /java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.Seq at this line
  val value = dbObject.as[String](key) /
) yield new MyClass(key, value)

有什么建议吗?

My document looks like this:

{
  "dynamic_field" : "...",
  "another_dynamic_field" : "..."
  "yet_another_dynamic_field" : "..."
}

"Dynamic field" means that I don't know it's name. So I want to get collection of strings which holds keys of this document. Then get values by of document by keys (structure of values are well defined).

So, I tried to do the following

val dbObject = ... 
val keys = dbObject.keys()
for(
  key <- keys; /java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.Seq at this line
  val value = dbObject.as[String](key) /
) yield new MyClass(key, value)

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-11-16 22:30:03

我不确定你的铸造会发生什么问题,但请确保你在范围内有正确的隐式:

import com.mongodb.casbah.Imports._

我根本不确定“BasicDBList”来自哪里,但我认为你的迭代过于复杂化。只要隐式在范围内,您就可以将 DBObject 直接视为 Scala 对象。这意味着您可以立即迭代它的键和值:

scala> val doc = conn("worldDevelopmentIndicators.in").findOne().get
doc: com.mongodb.DBObject = { "_id" : "4ddab3c62511cea643f3e5a0" , "SeriesCode" : "AG.AGR.TRAC.NO" , "Series Name" : "\"Agricultural machinery" , "Country Code" : " tractors\"" , "Country Name" : "AFG" , "1960" : "Afghanistan" , "1962" : 120.0 , "1963" : 150.0 , "1964" : 200.0 , "1965" : 200.0 , "1966" : 300.0 , "1967" : 400.0 , "1968" : 500.0 , "1969" : 500.0 , "1970" : 550.0 , "1971" : 550.0 , "1972" : 600.0 , "1973" : 600.0 , "1974" : 585.0 , "1975" : 570.0 , "1976" : 550.0 , "1977" : 530.0 , "1978" : 515.0 , "1979" : 495.0 , "1980" : 450.0 , "1981" : 400.0 , "1982" : 350.0 , "1983" : 300.0 , "1984" : 250.0 , "1985" : 200.0 , "1986" : 150.0 , "1987" : 150.0 , "1988" : 120.0 , "1989" : 120.0 , "1990" : 120.0 , "1991" : 120.0 , "1992" : 120.0 , "1993" : 110.0 , "1994" : 110.0 , "1995" ...

for ((k,v) <- doc) println(k) 
/*
 _id
 SeriesCode
 Series Name
 Country Code
 Country Name
*/

// you also have the value in this iteration in V...
scala> for (kv <- doc) println(kv) 
(_id,4ddab3c62511cea643f3e5a0)
(SeriesCode,AG.AGR.TRAC.NO)
(Series Name,"Agricultural machinery)
(Country Code, tractors")
(Country Name,AFG)
(1960,Afghanistan)

I'm not sure what's breaking there as far as your casting goes, but make sure you have the right implicits in scope:

import com.mongodb.casbah.Imports._

I am not sure at all where that "BasicDBList" is coming from, but I think you're overcomplicating your iteration. As long as the implicits are in scope you can treat the DBObject directly as a Scala object. That means you can iterate its keys and values immediately:

scala> val doc = conn("worldDevelopmentIndicators.in").findOne().get
doc: com.mongodb.DBObject = { "_id" : "4ddab3c62511cea643f3e5a0" , "SeriesCode" : "AG.AGR.TRAC.NO" , "Series Name" : "\"Agricultural machinery" , "Country Code" : " tractors\"" , "Country Name" : "AFG" , "1960" : "Afghanistan" , "1962" : 120.0 , "1963" : 150.0 , "1964" : 200.0 , "1965" : 200.0 , "1966" : 300.0 , "1967" : 400.0 , "1968" : 500.0 , "1969" : 500.0 , "1970" : 550.0 , "1971" : 550.0 , "1972" : 600.0 , "1973" : 600.0 , "1974" : 585.0 , "1975" : 570.0 , "1976" : 550.0 , "1977" : 530.0 , "1978" : 515.0 , "1979" : 495.0 , "1980" : 450.0 , "1981" : 400.0 , "1982" : 350.0 , "1983" : 300.0 , "1984" : 250.0 , "1985" : 200.0 , "1986" : 150.0 , "1987" : 150.0 , "1988" : 120.0 , "1989" : 120.0 , "1990" : 120.0 , "1991" : 120.0 , "1992" : 120.0 , "1993" : 110.0 , "1994" : 110.0 , "1995" ...

for ((k,v) <- doc) println(k) 
/*
 _id
 SeriesCode
 Series Name
 Country Code
 Country Name
*/

// you also have the value in this iteration in V...
scala> for (kv <- doc) println(kv) 
(_id,4ddab3c62511cea643f3e5a0)
(SeriesCode,AG.AGR.TRAC.NO)
(Series Name,"Agricultural machinery)
(Country Code, tractors")
(Country Name,AFG)
(1960,Afghanistan)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文