/** * Performs an update. If query will match a document, then it will be modified and old or new * version of document returned (depending if {@link #returningNew()} was configured). When * there * isn't any matching document, {@link Optional#absent()} will be result of the operation. * @return future of optional document (present if matching document would be found) */ public final FluentFuture<Optional<T>> update() { options.sort(convertToBson(ordering)); // TODO exlusion / projection return repository.doModify(criteria, collectRequiredUpdate(), options); } }
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; } }
/** * Performs an upsert. If query will match a document, then it will be modified and old or new * version of document returned (depending if {@link #returningNew()} was configured). When * there * isn't any such matching document, a new one will be created and returned if * {@link #returningNew()} was configured. * <p> * <em>Note: Upsert operation requires special care to set or init all required attributes * (including but not limited to '_id'), so that valid document could be inserted into collection. * </em> * @return future of optional document. * */ public final FluentFuture<Optional<T>> upsert() { options.upsert(true); options.sort(convertToBson(ordering)); // TODO exclusion / projection return repository.doModify(criteria, collectRequiredUpdate(), options); }
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); } }
/** * Performs an update. If query will match a document, then it will be modified and old or new * version of document returned (depending if {@link #returningNew()} was configured). When * there * isn't any matching document, {@link Optional#absent()} will be result of the operation. * @return future of optional document (present if matching document would be found) */ public final FluentFuture<Optional<T>> update() { options.sort(convertToBson(ordering)); // TODO exlusion / projection return repository.doModify(criteria, collectRequiredUpdate(), options); } }
/** * Performs an upsert. If query will match a document, then it will be modified and old or new * version of document returned (depending if {@link #returningNew()} was configured). When * there * isn't any such matching document, a new one will be created and returned if * {@link #returningNew()} was configured. * <p> * <em>Note: Upsert operation requires special care to set or init all required attributes * (including but not limited to '_id'), so that valid document could be inserted into collection. * </em> * @return future of optional document. * */ public final FluentFuture<Optional<T>> upsert() { options.upsert(true); options.sort(convertToBson(ordering)); // TODO exclusion / projection return repository.doModify(criteria, collectRequiredUpdate(), options); }
@Test public void testFindOneAndUpdateSorted() throws Exception { collection.insertOne(json("_id: 1, a: 15")); collection.insertOne(json("_id: 2, a: 10")); collection.insertOne(json("_id: 3, a: 20")); Document order = json("a: 1"); Document result = collection.findOneAndUpdate(json(""), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().sort(order).returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 2, a: 11")); order = json("a: -1"); result = collection.findOneAndUpdate(json(""), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().sort(order).returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 3, a: 21")); }
/** * 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)); }
} else { FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions() .sort(sort) .projection(projection) .upsert(upsert)
.sort( sort ) .bypassDocumentValidation( ( bypass != null ? bypass : false ) ) .upsert( ( upsert != null ? upsert : false ) )
.sort( sort ) .bypassDocumentValidation( ( bypass != null ? bypass : false ) ) .upsert( ( upsert != null ? upsert : false ) )