문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top