@Override public <S extends T> Mono<S> insert(S entity) { Assert.notNull(entity, "Entity must not be null!"); return mongoOperations.insert(entity, entityInformation.getCollectionName()); }
@Override public Mono<T> findById(ID id) { Assert.notNull(id, "The given id must not be null!"); return mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName()); }
@Override public Flux<T> findAllById(Iterable<ID> ids) { Assert.notNull(ids, "The given Iterable of Id's must not be null!"); return findAll(new Query(new Criteria(entityInformation.getIdAttribute()) .in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList())))); }
@Override public <S extends T> S save(S entity) { Assert.notNull(entity, "Entity must not be null!"); if (entityInformation.isNew(entity)) { return mongoOperations.insert(entity, entityInformation.getCollectionName()); } return mongoOperations.save(entity, entityInformation.getCollectionName()); }
private Flux<T> findAll(Query query) { return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName()); } }
@Override public void delete(T entity) { Assert.notNull(entity, "The given entity must not be null!"); deleteById(entityInformation.getRequiredId(entity)); }
@Override public <S extends T> List<S> findAll(Example<S> example, Sort sort) { Assert.notNull(example, "Sample must not be null!"); Assert.notNull(sort, "Sort must not be null!"); Query q = new Query(new Criteria().alike(example)).with(sort); return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public Mono<Void> deleteById(ID id) { Assert.notNull(id, "The given id must not be null!"); return mongoOperations .remove(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName()).then(); }
@Override public <S extends T> Mono<S> save(S entity) { Assert.notNull(entity, "Entity must not be null!"); if (entityInformation.isNew(entity)) { return mongoOperations.insert(entity, entityInformation.getCollectionName()); } return mongoOperations.save(entity, entityInformation.getCollectionName()); }
@Override public Mono<Boolean> existsById(ID id) { Assert.notNull(id, "The given id must not be null!"); return mongoOperations.exists(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName()); }
private boolean doUpdate(Object id, DBObject data, String... fieldsToDelete) { Assert.notNull(id); data.removeField("_id"); Update update = new Update(); setField("", data, update); for (String field : fieldsToDelete) { update.unset(field); } WriteResult result = mongoOperations.updateFirst(Query.query(Criteria.where("_id").is(id)), update, metadata.getCollectionName()); return result.getN() != 0; }
@Override public <S extends T> Flux<S> findAll(Example<S> example, Sort sort) { Assert.notNull(example, "Sample must not be null!"); Assert.notNull(sort, "Sort must not be null!"); Query query = new Query(new Criteria().alike(example)).with(sort); return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName()); }
@Override public <S extends T> Mono<S> findOne(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); q.limit(2); return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()).buffer(2) .map(vals -> { if (vals.size() > 1) { throw new IncorrectResultSizeDataAccessException(1); } return vals.iterator().next(); }).next(); }
@Override public void deleteById(ID id) { Assert.notNull(id, "The given id must not be null!"); mongoOperations.remove(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName()); }
@Override public Optional<T> findById(ID id) { Assert.notNull(id, "The given id must not be null!"); return Optional.ofNullable( mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName())); }