Sometimes I read a piece of code written by experts that raises a big red flag because it clearly is concerned about things that I know nothing about - and probably should.
In this case, here's the implementation of reactivemongo's BSONCollection.save(), which does an upsert based on ObjectId:
def save(doc: BSONDocument, writeConcern: GetLastError)(implicit ec: ExecutionContext): Future[LastError] = {
doc.get("_id").map { id =>
update(BSONDocument("_id" -> id), doc, writeConcern, upsert = true)
}.getOrElse(insert(doc.add("_id" -> BSONObjectID.generate), writeConcern))
}
Note the get() method is used to first determine whether the document exists, and then either an update-upsert or an insert is done, depending. But AFAIK, the update() method alone (note "upsert=true") would do exactly the same thing. What am I missing?
I'm guessing that the same concerns (whatever they are) also apply to any upsert with a unique-index "find" clause - not just ObjectId. Is that correct?