4

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

Vinicius Miana
  • 2,047
  • 17
  • 27
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • Can you make a check? Replace `listObjects` with `listObjects.asInstanceOf[List[com.mongodb.casba h.Imports.DBObject]]`. Will it give the same error? – Arseniy Zhizhelev Sep 27 '13 at 18:41
  • Same error: `[error] Test:139: No implicit view available from List[com.mongodb.casbah.Imports.DBObject] => com.mongodb.casbah.Imports.DBObject.` – Kevin Meredith Sep 27 '13 at 18:49
  • @ViniciusMiana - isn't `List[DBObject]` sufficient for the type (shown as a comment)? If not, please let me know what to add. thanks. – Kevin Meredith Sep 27 '13 at 18:53

1 Answers1

6

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.

Community
  • 1
  • 1
Vinicius Miana
  • 2,047
  • 17
  • 27
  • Could you please tell me where to find the implicit conversion? I would've expected this import to handle it`import com.mongodb.casbah.Imports._` based on this Casbah tutorial. "it provides an Imports object which automatically imports everything you need including Implicit conversions and type aliases to a few common MongoDB types" (http://api.mongodb.org/scala/casbah/2.0/tutorial.html) – Kevin Meredith Sep 27 '13 at 18:58
  • Yes, I'm using that import. – Kevin Meredith Sep 27 '13 at 19:07
  • That got me around my issue, but now I'm seeing `BasicBSONList can only work with numeric keys, not: [_id]`, which may be the topic of another post. Thanks – Kevin Meredith Sep 27 '13 at 19:38
  • Also, this worked too: `collection.insert(MongoDBList(listObjects))`. Why was the `:_*` used? – Kevin Meredith Sep 27 '13 at 19:43
  • 1
    @Kevin: To get the elements instead of the list, otherwise you you will end up with a list of lists. – Vinicius Miana Sep 27 '13 at 22:51
  • No, :_* turn a List object into a list of arguments. – Vinicius Miana Sep 28 '13 at 14:58
  • Hey Vinicius - quick follow-up question: does inserting via the above answer result in inserting claims one at a time? Or via a bulk insertion? – Kevin Meredith Oct 26 '13 at 02:04