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);
            }
        }
예제 #2
0
        /// <inheritdoc />
        public virtual UpdateResult UpdateMany(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 UpdateManyModel <TDocument>(filter, update)
            {
                Collation = options.Collation,
                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);
            }
        }
        /// <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);
            }
        }
        /// <inheritdoc />
        public virtual async Task <ReplaceOneResult> ReplaceOneAsync(FilterDefinition <TDocument> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull((object)replacement, "replacement");
            if (options?.ArrayFilters != null)
            {
                throw new ArgumentException("ArrayFilters cannot be used with ReplaceOne.", nameof(options));
            }

            options = options ?? new UpdateOptions();
            var model = new ReplaceOneModel <TDocument>(filter, replacement)
            {
                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(ReplaceOneResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #5
0
        private UpdateResult UpdateMany(FilterDefinition <TDocument> filter, UpdateDefinition <TDocument> update, UpdateOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, BulkWriteResult <TDocument> > bulkWrite)
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull(update, nameof(update));

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

            try
            {
                var bulkWriteOptions = new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation,
                    Let = options.Let
                };
                var result = bulkWrite(new[] { model }, bulkWriteOptions);
                return(UpdateResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #6
0
        private async Task <DeleteResult> DeleteOneAsync(FilterDefinition <TDocument> filter, DeleteOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, Task <BulkWriteResult <TDocument> > > bulkWriteAsync)
        {
            Ensure.IsNotNull(filter, nameof(filter));
            options = options ?? new DeleteOptions();

            var model = new DeleteOneModel <TDocument>(filter)
            {
                Collation = options.Collation,
                Hint      = options.Hint
            };

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

                return(DeleteResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #7
0
        private DeleteResult DeleteOne(FilterDefinition <TDocument> filter, DeleteOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, BulkWriteResult> bulkWrite)
        {
            Ensure.IsNotNull(filter, nameof(filter));
            options = options ?? new DeleteOptions();

            var model = new DeleteOneModel <TDocument>(filter)
            {
                Collation = options.Collation,
                Hint      = options.Hint
            };

            try
            {
                var bulkWriteOptions = new BulkWriteOptions
                {
                    Let = options.Let
                };
                var result = bulkWrite(new[] { model }, bulkWriteOptions);
                return(DeleteResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #8
0
        private ReplaceOneResult ReplaceOne(FilterDefinition <TDocument> filter, TDocument replacement, UpdateOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, BulkWriteResult <TDocument> > bulkWrite)
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull((object)replacement, "replacement");
            if (options?.ArrayFilters != null)
            {
                throw new ArgumentException("ArrayFilters cannot be used with ReplaceOne.", nameof(options));
            }

            options = options ?? new UpdateOptions();
            var model = new ReplaceOneModel <TDocument>(filter, replacement)
            {
                Collation = options.Collation,
                IsUpsert  = options.IsUpsert
            };

            try
            {
                var bulkWriteOptions = new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation
                };
                var result = bulkWrite(new[] { model }, bulkWriteOptions);
                return(ReplaceOneResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #9
0
        /// <inheritdoc />
        public virtual ReplaceOneResult ReplaceOne(FilterDefinition <TDocument> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull((object)replacement, "replacement");

            options = options ?? new UpdateOptions();
            var model = new ReplaceOneModel <TDocument>(filter, replacement)
            {
                Collation = options.Collation,
                IsUpsert  = options.IsUpsert
            };

            try
            {
                var bulkWriteOptions = new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation
                };
                var result = BulkWrite(new[] { model }, bulkWriteOptions, cancellationToken);
                return(ReplaceOneResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
        private async Task <ReplaceOneResult> ReplaceOneAsync(FilterDefinition <TDocument> filter, TDocument replacement, ReplaceOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, Task <BulkWriteResult <TDocument> > > bulkWriteAsync)
        {
            Ensure.IsNotNull(filter, nameof(filter));
            Ensure.IsNotNull((object)replacement, "replacement");

            options = options ?? new ReplaceOptions();
            var model = new ReplaceOneModel <TDocument>(filter, replacement)
            {
                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(ReplaceOneResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #11
0
 public static void ThrowIfDuplicateKey(this MongoWriteException ex, string fieldKey, string errorMessage)
 {
     if (ex.WriteError != null && ex.WriteError.Category == ServerErrorCategory.DuplicateKey && ex.WriteError.Code == 11000)
     {
         var alreadyExistsException = new AlreadyExistsException();
         alreadyExistsException.AddModelError(fieldKey, errorMessage);
         throw alreadyExistsException;
     }
 }
        public static string ExtactViolatedIndexNameFrom(MongoWriteException exception)
        {
            if (exception.WriteError.Category != ServerErrorCategory.DuplicateKey)
                throw new ArgumentException("WriteError.Category should be DuplicateKey", nameof(exception));

            var message = exception.WriteError.Message;
            var startIndex = message.IndexOf(".$") + 2;
            var endIndex = message.IndexOf(" dup key");

            return message.Substring(startIndex, endIndex - startIndex);
        }
예제 #13
0
        /// <inheritdoc />
        public virtual async Task InsertOneAsync(TDocument document, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull((object)document, "document");

            var model = new InsertOneModel <TDocument>(document);

            try
            {
                await BulkWriteAsync(new[] { model }, null, cancellationToken).ConfigureAwait(false);
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #14
0
        /// <inheritdoc />
        public virtual DeleteResult DeleteMany(FilterDefinition <TDocument> filter, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(filter, nameof(filter));

            var model = new DeleteManyModel <TDocument>(filter);

            try
            {
                var result = BulkWrite(new[] { model }, null, cancellationToken);
                return(DeleteResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #15
0
        /// <inheritdoc />
        public virtual async Task <DeleteResult> DeleteOneAsync(FilterDefinition <TDocument> filter, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(filter, "filter");

            var model = new DeleteOneModel <TDocument>(filter);

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

                return(DeleteResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
        private void InsertOne(TDocument document, InsertOneOptions options, Action <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions> bulkWrite)
        {
            Ensure.IsNotNull((object)document, "document");

            var model = new InsertOneModel <TDocument>(document);

            try
            {
                var bulkWriteOptions = options == null ? null : new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation
                };
                bulkWrite(new[] { model }, bulkWriteOptions);
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
        /// <inheritdoc />
        public virtual async Task InsertOneAsync(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull((object)document, "document");

            var model = new InsertOneModel <TDocument>(document);

            try
            {
                var bulkWriteOptions = options == null ? null : new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation
                };
                await BulkWriteAsync(new[] { model }, bulkWriteOptions, cancellationToken).ConfigureAwait(false);
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
        private async Task InsertOneAsync(TDocument document, InsertOneOptions options, Func <IEnumerable <WriteModel <TDocument> >, BulkWriteOptions, Task> bulkWriteAsync)
        {
            Ensure.IsNotNull((object)document, "document");

            var model = new InsertOneModel <TDocument>(document);

            try
            {
                var bulkWriteOptions = options == null ? null : new BulkWriteOptions
                {
                    BypassDocumentValidation = options.BypassDocumentValidation
                };
                await bulkWriteAsync(new[] { model }, bulkWriteOptions).ConfigureAwait(false);
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #19
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);
            }
        }
예제 #20
0
        public async Task <ReplaceOneResult> ReplaceOneAsync(object filter, TDocument replacement, UpdateOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(filter, "filter");
            Ensure.IsNotNull((object)replacement, "replacement");

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

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

                return(ReplaceOneResult.FromCore(result));
            }
            catch (MongoBulkWriteException <TDocument> ex)
            {
                throw MongoWriteException.FromBulkWriteException(ex);
            }
        }
예제 #21
0
        public async Task <UpdateResult> UpdateManyAsync(object filter, object update, UpdateOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(filter, "filter");
            Ensure.IsNotNull(update, "update");

            options = options ?? new UpdateOptions();
            var model = new UpdateManyModel <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);
            }
        }