没有可用的隐式视图 [英] No Implicit View Available

查看:169
本文介绍了没有可用的隐式视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试通过DBObject的列表时. html"rel =" nofollow>插入,我得到no implicit view available.

When trying to bulk load a list of DBObject's via insert, I get no implicit view available.

collection.insert(listObjects) // listObjects is a List[DBObject]

[error]Test.scala:139: No implicit view available from List[com.mongodb.casba
h.Imports.DBObject] => com.mongodb.casbah.Imports.DBObject.

此错误是什么意思?我该如何解决?

What does this error mean? How can I resolve?

参考:

def insert [A] (docs: List[A])(implicit arg0: (A) ⇒ DBObject) : WriteResult

推荐答案

方法insert将获取任何List,但是casbah要将数据存储在Mongo中,需要将其转换为DBObject.为此,它使用隐式转换,该转换在casbah中可用于各种数据类型.但是,您尝试插入的数据在您的范围内没有实现或可用的转换.为了解决这个问题,要么导入隐式转换器,要么实现一个.

The method insert will take any List, but to store the data in Mongo, casbah needs to convert it to DBObject. To do that it uses an implicit conversion, which is available in casbah for various data-types. However the data you are trying to insert does not have a conversion implemented or available in your scope. To solve that either import the implicit converter or implement one.

在您的情况下,您可能缺少进口货.确保您有:

In your case you may be missing an import. Make sure you got:

import com.mongodb.casbah.Imports._

并尝试将listObjects替换为MongoDBList(listObjects:_*)

要回答您的评论,请尝试使用REPL:

To answer to your comment try in REPL:

scala> val a = List(1,2,3,4,5,6)
a: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> List(a:_*)
res0: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> List(a)
res1: List[List[Int]] = List(List(1, 2, 3, 4, 5, 6))

:_ * 将会获取元素列表,并避免创建列表列表.

The :_* will get the elements instead of the list and avoid creating a List of List.

这篇关于没有可用的隐式视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆