/// <summary>
        /// Finds a single document and updates it atomically.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="filter">The filter.</param>
        /// <param name="update">The update.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The returned document.
        /// </returns>
        public static Task <TDocument> FindOneAndUpdateAsync <TDocument>(this IMongoCollection <TDocument> collection, FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, FindOneAndUpdateOptions <TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(collection, "collection");
            Ensure.IsNotNull(collection, "filter");

            return(collection.FindOneAndUpdateAsync <TDocument>(
                       filter,
                       update,
                       options,
                       cancellationToken));
        }
        /// <summary>
        /// Updates a single document.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="filter">The filter.</param>
        /// <param name="update">The update.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The result of the update operation.
        /// </returns>
        public static Task <UpdateResult> UpdateOneAsync <TDocument>(this IMongoCollection <TDocument> collection, Expression <Func <TDocument, bool> > filter, UpdateDefinition <TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(collection, "collection");
            Ensure.IsNotNull(filter, "filter");

            return(collection.UpdateOneAsync(
                       new ExpressionFilterDefinition <TDocument>(filter),
                       update,
                       options,
                       cancellationToken));
        }
Example #3
0
        /// <inheritdoc />
        public virtual async Task <UpdateResult> UpdateOneAsync(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull(update, nameof(update));

            options = options ?? new UpdateOptions();
            var model = new UpdateOneModel <TDocument>(filter, update)
            {
                IsUpsert = options.IsUpsert
            };

            try
            {
                var bulkWriteOptions = new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation
                };
                var result = await BulkWriteAsync(new[] { model }, bulkWriteOptions, cancellationToken).ConfigureAwait(false);

                return(UpdateResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
Example #4
0
 /// <inheritdoc />
 public abstract Task <TProjection> FindOneAndUpdateAsync <TProjection>(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, FindOneAndUpdateOptions <TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken));
 public override Task <TProjection> FindOneAndUpdateAsync <TProjection>(IClientSessionHandle session, FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, FindOneAndUpdateOptions <TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_wrappedCollection.FindOneAndUpdateAsync(session, CombineFilters(filter), update, options, cancellationToken));
 }
 public override TProjection FindOneAndUpdate <TProjection>(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, FindOneAndUpdateOptions <TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_wrappedCollection.FindOneAndUpdate(CombineFilters(filter), update, options, cancellationToken));
 }
Example #7
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="UpdateOneModel{TDocument}"/> class.
 /// </summary>
 /// <param name="filter">The filter.</param>
 /// <param name="update">The update.</param>
 public UpdateOneModel(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update)
 {
     _filter = Ensure.IsNotNull(filter, nameof(filter));
     _update = Ensure.IsNotNull(update, nameof(update));
 }