/// <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)
            {
                ArrayFilters = options.ArrayFilters,
                Collation    = options.Collation,
                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);
            }
        }
        private async Task <UpdateResult> UpdateOneAsync(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, UpdateOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, Task <BulkWriteResult <TDocument> > > bulkWriteAsync)
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull(update, nameof(update));

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

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

                return(UpdateResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
Esempio n. 3
0
        /// <inheritdoc />
        public virtual UpdateResult UpdateOne(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 = BulkWrite(new[] { model }, bulkWriteOptions, cancellationToken);
                return(UpdateResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
Esempio n. 4
0
        public async Task <UpdateResult> UpdateOneAsync(UpdateOneModel <TDocument> model, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(model, "model");

            try
            {
                var bulkModel = new BulkWriteModel <TDocument>(new[] { model });
                var result    = await BulkWriteAsync(bulkModel, timeout, cancellationToken);

                return(UpdateResult.FromCore(result));
            }
            catch (BulkWriteException <TDocument> ex)
            {
                throw WriteException.FromBulkWriteException(ex);
            }
        }
Esempio n. 5
0
        /// <inheritdoc />
        public virtual async Task <UpdateResult> UpdateOneAsync(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(filter, "filter");
            Ensure.IsNotNull(update, "update");

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

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

                return(UpdateResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }