Frage

I am wrting an update query on Scala and Cashbah. When I write this...

val query = MongoDBObject({"_id" -> uri.toString})

val update: DBObject =
  $set("uDate" -> new DateTime) ++
  $addToSet("appearsOn") $each(sourceToAppend:_*) ++
  $addToSet("hasElements") $each(elementsToAppend:_*) ++
  $addToSet("hasTriples") $each (triplesToAppend:_*)

OntologyDocument.dao.collection.update(query, update, upsert = true)

...it gives me a compilation error like this. But if I write it like this...

No implicit view available from Object{def $each(target: Array[Any]): com.mongodb.casbah.commons.Imports.DBObject; def $each(target: Any*): com.mongodb.casbah.commons.Imports.DBObject} => com.mongodb.casbah.commons.Imports.DBObject.

...then it compiles and runs successfully.

val query = MongoDBObject({"_id" -> uri.toString})

val setOps1 = $addToSet("appearsOn") $each(sourceToAppend:_*)
val setOps2 = $addToSet("hasElements") $each(elementsToAppend:_*)
val setOps3 = $addToSet("hasTriples") $each (triplesToAppend:_*)

val update: DBObject = $set("uDate" -> new DateTime) ++ setOps1 ++ setOps2 ++ setOps3

OntologyDocument.dao.collection.update(query, update, upsert = true)

I didn't get it.

Why doesn't the first one compile, or the second one compiles?

Am I missing a Scala basic?

War es hilfreich?

Lösung

This is due to how the dsl is implemented and the chaining of $each. The core issue is I dont think $addToSet merges as expected::

val setOps1 = $addToSet("appearsOn") $each(sourceToAppend:_*)
val setOps2 = $addToSet("hasElements") $each(elementsToAppend:_*)
setOps1 ++ setOps2

DBObject = { "$addToSet" : { "hasElements" : { "$each" : [ "another" , "list"]}}}

Seems MongoDBObject ++ does not recursively merge.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top