如何通过casbah获取文档的key?
我的文档如下所示:
{
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你的铸造会发生什么问题,但请确保你在范围内有正确的隐式:
我根本不确定“BasicDBList”来自哪里,但我认为你的迭代过于复杂化。只要隐式在范围内,您就可以将 DBObject 直接视为 Scala 对象。这意味着您可以立即迭代它的键和值:
I'm not sure what's breaking there as far as your casting goes, but make sure you have the right implicits in scope:
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: