No Implicit View Available

丶灬走出姿态 提交于 2019-12-14 01:07:21

问题


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?

Reference:

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


回答1:


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._

and try replacing listObjects by MongoDBList(listObjects:_*)

EDIT:

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.



来源:https://stackoverflow.com/questions/19057695/no-implicit-view-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!