I'm writing a generic update method to simplify save an case class change to mongodb. my model T
trait has the following function:
def update(id: BSONObjectID, t: T)(implicit writer: OFormat[T]): Future[WriteResult] = {
collection.update(Json.obj("_id" -> id), t)
}
when i'm calling it, it fails with the following error:
Caused by: reactivemongo.api.commands.UpdateWriteResult:
DatabaseException['The _id field cannot be changed from {_id: ObjectId('4ec58120cd6cad6afc000001')} to {_id: "4ec58120cd6cad6afc000001"}.' (code = 16837)]
Which makes sense cause MongoDB does not allow to update the document ID even though its the same value.
I'm wondering how i would remove the _id from my case-class instance to update it in mongodb. I guess I have to tuple the instance before it is converted to BSON, but i don't know how to do that. this is my example case class:
case class User(
_id: BSONObjectID,
email: String
}
thanks