AbstractMongoDao extends AbstractDao<M,P,K,I> { protected Document toUpdateDocument(Update<P> update, Instant updateAt, I updateBy) { Document doc = new Document(); Document setFields = partialMapper.toDocument(update.getPartial()); Document set = new Document(); for (String field : update.getSetFields()) { set.put(field, setFields.get(field)); } FieldData fieldData = getFieldData(); if (updateAt != null && fieldData.getUpdatedAt() != null) { set.put(fieldData.getUpdatedAt().getStorageName(), Date.from(updateAt)); } if (updateBy != null && fieldData.getUpdatedBy() != null) { set.put(fieldData.getUpdatedBy().getStorageName(), updateBy); } Document unset = new Document(); for (String field : update.getRemoveFields()) { unset.put(field, 1); } if (!set.isEmpty()) { doc.put("$set", set); } if (!unset.isEmpty()) { doc.put("$unset", unset); } return doc; } AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection);  AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection, ObjectMapper objectMapper, ObjectMapper extendedJsonMapper);  AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection,
                            DocumentMapper<M> modelMapper, DocumentMapper<P> partialMapper); MongoCollection<Document> getCollection(); String getCollectionName(); DocumentMapper<M> getModelMapper(); DocumentMapper<P> getPartialMapper(); @Override Optional<M> loadOpt(K id); Optional<P> loadOpt(K id, Iterable<String> fields); @Override Iterable<M> iterateAll(); @Override M create(P partial, Instant createAt, I createBy); @Override void update(K id, Update<P> update, Instant updateAt, I updateBy); @Override M updateAndReturn(K id, Update<P> update, Instant updateAt, I updateBy); @Override void delete(K id); @Override void initTable(); @Override void dropTable(); List<IndexModel> getIndexes(); Update<P> updateOf(P object); Update<P> updateOf(P object, Iterable<String> removeFields); static final String ID_FIELD; }
@Test public void testToUpdateDocument() throws Exception { Update<TestUser> update = Update.of( TestUser.builder().birthday(LocalDate.of(1980, 5, 12)).build(), ImmutableSet.of("birthday"), ImmutableSet.of("email")); Instant now = Instant.now(); ObjectId updater = new ObjectId(); Document doc = dao.toUpdateDocument(update, now, updater); Document expected = new Document("$set", new Document("birthday", "1980-05-12") .append("updateAt", Date.from(now)) .append("updateId", updater)) .append("$unset", new Document("email", 1)); assertEquals(expected, doc); }