I am both new to scala and cashbah. I am trying to
- update a document if exists (by _id) and create if doesn't exist.
- while updating, update some key values
- while updating, update some keys which values are sets, include some data to those sets.
To achive this, I've written this:
DBObject = MongoDBObject("_id" -> uri.toString) ++
$addToSet("appearsOn" -> sourceToAppend) ++
$addToSet("hasElements" -> elementsToAppend) ++
$addToSet("hasTriples" -> triplesToAppend) ++
MongoDBObject("uDate" -> new DateTime)
/* Find and replace here! */
OntologyDocument.dao.collection.findAndModify(
query = MongoDBObject({"_id" -> uri.toString}),
update = update,
upsert = true,
fields = null,
sort = null,
remove = false,
returnNew = true
)
Documents looked by _id
, some new items added to appearsOn
hasElements
hasTriples
and uDate
is updated.
sourceToAppend
elementsToAppend
and triplesToAppend
are List[String]
When I run this, I got this error:
java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$addToSet')
at com.mongodb.DBCollection.validateKey(DBCollection.java:1444) ~[mongo-java-driver-2.11.1.jar:na]
I didn't get it. What is wrong with this query? $addToSet
isn't a field, why casbah thinks it is a field? What am I doing wrong here?