public long size() { return collection().count(); }
protected MongoCollection<Document> collectionWithWriteTimeout(long maxTime, TimeUnit timeUnit) { return collection() .withWriteConcern(collection().getWriteConcern().withWTimeout(maxTime, timeUnit)); }
protected MongoCollection<Document> collectionWithWriteTimeout(long maxTime, TimeUnit timeUnit) { return collection() .withWriteConcern(collection().getWriteConcern().withWTimeout(maxTime, timeUnit)); }
public void delete(final K key) { collection().deleteOne(byId(key)); }
public List<V> findAll() { return collection() .find() .map(this::decode) .into(new ArrayList<>()); }
public Stream<V> findAllAsStream(final long maxTime, final TimeUnit timeUnit) { return toStream( collection().find() .maxTime(maxTime, timeUnit)) .map(this::decode); }
public void deleteAll() { collection().deleteMany(matchAll()); }
public Stream<V> findAllAsStream(final long maxTime, final TimeUnit timeUnit) { return toStream( collection().find() .maxTime(maxTime, timeUnit)) .map(this::decode); }
public Optional<V> findOne(final K key) { return ofNullable(collection() .find(byId(key)) .map(this::decode) .first()); }
private FindIterable<Document> getFindIterable(int skip, int limit) { return collection() .find() .skip(skip) .limit(limit); }
public V createOrUpdate(final V value) { final K key = keyOf(value); final Document existing = collection().find(byId(key)).first(); Document doc = encode(value); if (existing != null) { collection().replaceOne(byId(key), doc); } else { collection().insertOne(doc); } return decode(doc); }
public V create(final V value) { Document doc = encode(value); collection().insertOne(doc); return decode(doc); }
private FindIterable<Document> getFindIterable(int skip, int limit) { return collection() .find() .skip(skip) .limit(limit); }
public List<V> findAll(int skip, int limit) { return collection() .find() .skip(skip) .limit(limit) .map(this::decode) .into(new ArrayList<>()); }
/** * Find a single value with the specified key, if existing. * * @param key the key to search for * @param maxTime the maximum time for this request * @param timeUnit the time unit in which {@code maxTime} is specified * @return an Optional containing the requested value, or {@code Optional.empty()} if no value with this key exists */ public Optional<V> findOne(final K key, final long maxTime, final TimeUnit timeUnit) { return ofNullable(collection() .find(byId(key)) .maxTime(maxTime, timeUnit) .map(this::decode) .first()); }
public void update(final V value) { final K key = keyOf(value); collection().replaceOne(byId(key), encode(value)); }
/** * Find a single value with the specified key, if existing. * * @param key the key to search for * @param maxTime the maximum time for this request * @param timeUnit the time unit in which {@code maxTime} is specified * @return an Optional containing the requested value, or {@code Optional.empty()} if no value with this key exists */ public Optional<V> findOne(final K key, final long maxTime, final TimeUnit timeUnit) { return ofNullable(collection() .find(byId(key)) .maxTime(maxTime, timeUnit) .map(this::decode) .first()); }
public long size(final long maxTime, final TimeUnit timeUnit) { return collection().countDocuments(new BsonDocument(), new CountOptions().maxTime(maxTime, timeUnit)); }
public long size(final long maxTime, final TimeUnit timeUnit) { return collection().count(new BsonDocument(), new CountOptions().maxTime(maxTime, timeUnit)); }
public void updateIfMatch(final V value, final String eTag) { Bson query = and(eq(AbstractMongoRepository.ID, keyOf(value)), eq(ETAG, eTag)); Document updatedETaggable = collection().findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER)); if (isNull(updatedETaggable)) { Optional<V> findById = findOne(keyOf(value)); if (findById.isPresent()) { throw new ConcurrentModificationException("Entity concurrently modified: " + keyOf(value)); } throw new NotFoundException("Entity does not exist: " + keyOf(value)); } }