I have a collection in my MongoDB database with say several keys. Now I want to update this collection with a new field. So here is what I have so far:
def confirm(hash: String) = {
val myDb = dbConn.db(dbName)
val userCollection = myDb[BSONCollection]("user")
val selector = BSONDocument(idKey -> BSONObjectID(hash))
val modifier = BSONDocument(
"$set" -> BSONDocument("date" -> BSONString(now.toString)) // Line 1
)
val command = FindAndModify(
userCollection.name,
selector,
Update(modifier, fetchNewObject = true)
)
myDb.command(command)
.map { user => // Line 2
Right(bidList)
}.recover {
case LastError(ok,err, code, errMsg, _) =>
Left(ServiceError(errMsg.getOrElse("failure!")))
}
}
I have two problems with the above implementation:
On Line 1: Would this update the existing document with a new field called date?
On Line 2: Mapping the myDb.command(command) gives me an Option[BSONDocument], but what I'm surprised is that I have an implicit conversion in scope. So I would have expected it to return an Option[User]!