Пример #1
0
        private static WriteModel <T> ConvertToReplaceOne(UpdateRequest updateRequest)
        {
            object document;
            var    wrappedUpdate = updateRequest.Update as BsonDocumentWrapper;

            if (wrappedUpdate != null)
            {
                document = wrappedUpdate.Wrapped;
            }
            else
            {
                document = updateRequest.Update;
            }

            return(new ReplaceOneModel <T>(updateRequest.Criteria, (T)document)
            {
                IsUpsert = updateRequest.IsUpsert.GetValueOrDefault(false)
            });
        }
        public async Task UpdateOneAsync_should_execute_the_BulkMixedOperation(bool upsert)
        {
            var criteria        = BsonDocument.Parse("{a:1}");
            var update          = BsonDocument.Parse("{$set:{a:1}}");
            var expectedRequest = new UpdateRequest(UpdateType.Update, criteria, update)
            {
                CorrelationId = 0, IsUpsert = upsert, IsMulti = false
            };
            var operationResult = new BulkWriteOperationResult.Unacknowledged(9, new[] { expectedRequest });

            _operationExecutor.EnqueueResult <BulkWriteOperationResult>(operationResult);

            await _subject.UpdateOneAsync(
                new UpdateOneModel <BsonDocument>(criteria, update) { IsUpsert = upsert },
                Timeout.InfiniteTimeSpan,
                CancellationToken.None);

            var call = _operationExecutor.GetWriteCall <BulkWriteOperationResult>();

            VerifySingleWrite(expectedRequest, call);
        }
        public async Task UpdateManyAsync_should_execute_the_BulkMixedOperation(bool upsert)
        {
            var filter          = BsonDocument.Parse("{a:1}");
            var update          = BsonDocument.Parse("{$set:{a:1}}");
            var expectedRequest = new UpdateRequest(UpdateType.Update, filter, update)
            {
                CorrelationId = 0, IsUpsert = upsert, IsMulti = true
            };
            var operationResult = new BulkWriteOperationResult.Unacknowledged(9, new[] { expectedRequest });

            _operationExecutor.EnqueueResult <BulkWriteOperationResult>(operationResult);

            await _subject.UpdateManyAsync(
                filter,
                update,
                new UpdateOptions { IsUpsert = upsert },
                CancellationToken.None);

            var call = _operationExecutor.GetWriteCall <BulkWriteOperationResult>();

            VerifySingleWrite(expectedRequest, call);
        }
Пример #4
0
        private static WriteModel <T> ConvertUpdateRequest(UpdateRequest updateRequest)
        {
            if (updateRequest.IsMultiUpdate.GetValueOrDefault(false))
            {
                return(new UpdateManyModel <T>(updateRequest.Criteria, updateRequest.Update)
                {
                    IsUpsert = updateRequest.IsUpsert.GetValueOrDefault(false)
                });
            }

            var firstElement = updateRequest.Update.GetElement(0).Name;

            if (firstElement.StartsWith("$"))
            {
                return(new UpdateOneModel <T>(updateRequest.Criteria, updateRequest.Update)
                {
                    IsUpsert = updateRequest.IsUpsert.GetValueOrDefault(false)
                });
            }

            return(ConvertToReplaceOne(updateRequest));
        }
Пример #5
0
        private static WriteModel <TDocument> ConvertUpdateRequest(UpdateRequest request)
        {
            if (request.IsMulti)
            {
                return(new UpdateManyModel <TDocument>(UnwrapFilter(request.Filter), UnwrapUpdate(request.Update))
                {
                    IsUpsert = request.IsUpsert
                });
            }

            var firstElement = request.Update.GetElement(0).Name;

            if (firstElement.StartsWith("$"))
            {
                return(new UpdateOneModel <TDocument>(UnwrapFilter(request.Filter), UnwrapUpdate(request.Update))
                {
                    IsUpsert = request.IsUpsert
                });
            }

            return(ConvertToReplaceOne(request));
        }
Пример #6
0
        private static WriteModel <TDocument> ConvertUpdateRequest(UpdateRequest request)
        {
            if (request.IsMulti)
            {
                return(new UpdateManyModel <TDocument>(UnwrapFilter(request.Filter), UnwrapUpdate(request.Update))
                {
                    ArrayFilters = request.ArrayFilters == null ? null : new List <ArrayFilterDefinition>(request.ArrayFilters.Select(f => new BsonDocumentArrayFilterDefinition <BsonValue>(f))),
                    Collation = request.Collation,
                    IsUpsert = request.IsUpsert
                });
            }

            if (!IsReplaceOneUpdate(request.Update))
            {
                return(new UpdateOneModel <TDocument>(UnwrapFilter(request.Filter), UnwrapUpdate(request.Update))
                {
                    ArrayFilters = request.ArrayFilters == null ? null : new List <ArrayFilterDefinition>(request.ArrayFilters.Select(f => new BsonDocumentArrayFilterDefinition <BsonValue>(f))),
                    Collation = request.Collation,
                    IsUpsert = request.IsUpsert
                });
            }

            return(ConvertToReplaceOne(request));
        }