Map<String, List<ValidationResult>> errors = validate(model, model.getFields());
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
BasicDBObject doc = new BasicDBObject(model.getFields());
doc.put("_id", new ObjectId(model.getId())); // ID was created in constructor or taken from original doc already.
// Do field transformations
fieldTransformations(doc);
/*
* We are running an upsert. This means that the existing
* document will be updated if the ID already exists and
* a new document will be created if it doesn't.
*/
BasicDBObject q = new BasicDBObject("_id", new ObjectId(model.getId()));
collection(model).update(q, doc, true, false);
return model.getId();
}