private FindOneAndUpdateOptions convertToFindOneAndUpdateOptions(FindAndModifyOptions options, Document fields, Document sort) { FindOneAndUpdateOptions result = new FindOneAndUpdateOptions(); result = result.projection(fields).sort(sort).upsert(options.isUpsert()); if (options.isReturnNew()) { result = result.returnDocument(ReturnDocument.AFTER); } else { result = result.returnDocument(ReturnDocument.BEFORE); } result = options.getCollation().map(Collation::toMongoCollation).map(result::collation).orElse(result); return result; } }
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindOneAndUpdateOptions opts = new FindOneAndUpdateOptions(); opts.sort(sort); if (options.isUpsert()) { opts.upsert(true); } opts.projection(fields); if (options.isReturnNew()) { opts.returnDocument(ReturnDocument.AFTER); } options.getCollation().map(Collation::toMongoCollation).ifPresent(opts::collation); return collection.findOneAndUpdate(query, update, opts); } }
private FindOneAndUpdateOptions convertToFindOneAndUpdateOptions(FindAndModifyOptions options, Document fields, Document sort) { FindOneAndUpdateOptions result = new FindOneAndUpdateOptions(); result = result.projection(fields).sort(sort).upsert(options.isUpsert()); if (options.isReturnNew()) { result = result.returnDocument(ReturnDocument.AFTER); } else { result = result.returnDocument(ReturnDocument.BEFORE); } result = options.getCollation().map(Collation::toMongoCollation).map(result::collation).orElse(result); return result; } }
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindOneAndUpdateOptions opts = new FindOneAndUpdateOptions(); opts.sort(sort); if (options.isUpsert()) { opts.upsert(true); } opts.projection(fields); if (options.isReturnNew()) { opts.returnDocument(ReturnDocument.AFTER); } options.getCollation().map(Collation::toMongoCollation).ifPresent(opts::collation); return collection.findOneAndUpdate(query, update, opts); } }
private int getNextValueInSeq(final String counterName, final int numbers) { return MongoDbOperations.doDbOperation(() -> { Document andModify = counters.findOneAndUpdate(new Document("_id", counterName), new Document("$inc", new Document("seq", numbers)),new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER).projection(new Document("seq",1))); return (Integer) andModify.get("seq"); }); }
@Nonnull @Override public <V> Optional<V> valueFor(SingleFeatureBean feature, V value) { checkNotNull(feature, "feature"); checkNotNull(value, "value"); final String ownerId = idConverter.convert(feature.owner()); final String featureId = Integer.toString(feature.id()); final String fieldName = concat(ModelDocument.F_SINGLE_FEATURE, featureId); final Bson filter = eq(ModelDocument.F_ID, ownerId); final Bson projection = include(fieldName); final Bson update = set(fieldName, serializeValue(value)); final ModelDocument instance = documents.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions().upsert(true).projection(projection)); return Optional.ofNullable(instance) .map(ModelDocument::getSingleFeatures) .map(l -> l.get(featureId)) .map(this::deserializeValue); }
@Nonnull @Override public <V> Optional<V> valueFor(ManyFeatureBean feature, V value) { checkNotNull(feature, "feature"); checkNotNull(value, "value"); final String ownerId = idConverter.convert(feature.owner()); final String featureId = Integer.toString(feature.id()); final String fieldName = concat(ModelDocument.F_MANY_FEATURE, featureId); final String fieldNameWithPos = concat(fieldName, Integer.toString(feature.position())); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldNameWithPos)); final Bson projection = slice(fieldName, feature.position(), 1); final Bson update = set(fieldNameWithPos, serializeValue(value)); final ModelDocument instance = documents.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions().projection(projection)); final Optional<V> previousValue = Optional.ofNullable(instance) .map(ModelDocument::getManyFeatures) .map(m -> m.get(featureId)) .filter(l -> !l.isEmpty()) .map(l -> l.get(0)) .map(this::deserializeValue); if (!previousValue.isPresent()) { throw new IndexOutOfBoundsException(); } return previousValue; }
@Override public long decrement(String aCounterName) { Document doc = collection .findOneAndUpdate( eq("_id", aCounterName), inc(DSL.VALUE, -1), new FindOneAndUpdateOptions() .returnDocument(AFTER) .projection(include(DSL.VALUE)) ); if (doc == null) { throw new IllegalArgumentException("Counter not found: " + aCounterName); } return doc.getLong(DSL.VALUE); }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param update * update to apply. This must contain only update operators * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(Document query, Document fields, Document sort, Document update, boolean returnNew, boolean upsert) { return mongoCollection.findOneAndUpdate(serializeFields(query), update, new FindOneAndUpdateOptions().returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE).projection(fields).sort(sort).upsert(upsert)); }
public Document findAndUpdate(Bson query, Bson projection, Bson sort, Bson update, QueryOptions options) { boolean upsert = false; boolean returnNew = false; if (options != null) { if (projection == null) { projection = getProjection(projection, options); } upsert = options.getBoolean("upsert", false); returnNew = options.getBoolean("returnNew", false); } FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions() .sort(sort) .projection(projection) .upsert(upsert) .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE); return dbCollection.findOneAndUpdate(query, update, findOneAndUpdateOptions); }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(DBQuery.Query query, Document fields, Document sort, DBUpdate.Builder update, boolean returnNew, boolean upsert) { return mongoCollection.findOneAndUpdate(serializeQuery(query), update.serializeAndGetAsDocument(objectMapper, type), new FindOneAndUpdateOptions().returnDocument( returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE).projection(fields).sort(sort).upsert(upsert)); }
/** * Finds the first document in the query and updates it. * * @param query * query to match * @param fields * fields to be returned * @param sort * sort to apply before picking first document * @param update * update to apply * @param returnNew * if true, the updated document is returned, otherwise the old * document is returned (or it would be lost forever) * @param upsert * do upsert (insert if document not present) * @return the object */ public T findAndModify(Document query, Document fields, Document sort, DBUpdate.Builder update, boolean returnNew, boolean upsert) { return mongoCollection.findOneAndUpdate(serializeFields(query), update.serializeAndGetAsDocument(objectMapper, type), new FindOneAndUpdateOptions().returnDocument( returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE).projection(fields).sort(sort).upsert(upsert)); }
.projection(fields).upsert(true);
FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions() .sort(sort) .projection(projection) .upsert(upsert) .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
.bypassDocumentValidation( ( bypass != null ? bypass : false ) ) .upsert( ( upsert != null ? upsert : false ) ) .projection( fields ) .returnDocument( ( returnNewDocument != null ? returnNewDocument : false ) ? ReturnDocument.AFTER : ReturnDocument.BEFORE ) .maxTime( 0, TimeUnit.MILLISECONDS );
.bypassDocumentValidation( ( bypass != null ? bypass : false ) ) .upsert( ( upsert != null ? upsert : false ) ) .projection( fields ) .returnDocument( ( returnNewDocument != null ? returnNewDocument : false ) ? ReturnDocument.AFTER : ReturnDocument.BEFORE ) .maxTime( 0, TimeUnit.MILLISECONDS );