Beispiel #1
0
        public static Task <BulkWriteResult <T> > ReplaceManyAsync <T, TK>(this IMongoCollection <T> instance, T[] documents, CancellationToken cancellation = default)
            where T : DocumentBase <TK>
        {
            var bulkOps = new WriteModel <T> [documents.Length];

            for (var i = 0; i < documents.Length; i++)
            {
                var filter = Builders <T> .Filter.Eq(x => x.Id, documents[i].Id);

                bulkOps[i] = new ReplaceOneModel <T>(filter, documents[i]);
            }

            return(instance.BulkWriteAsync(bulkOps, cancellationToken: cancellation));
        }
        private WriteRequest ConvertWriteModelToWriteRequest(WriteModel <TDocument> model, int index)
        {
            switch (model.ModelType)
            {
            case WriteModelType.InsertOne:
                var insertOneModel = (InsertOneModel <TDocument>)model;
                AssignId(insertOneModel.Document);
                return(new InsertRequest(new BsonDocumentWrapper(insertOneModel.Document, _documentSerializer))
                {
                    CorrelationId = index
                });

            case WriteModelType.DeleteMany:
                var deleteManyModel = (DeleteManyModel <TDocument>)model;
                return(new DeleteRequest(deleteManyModel.Filter.Render(_documentSerializer, _settings.SerializerRegistry))
                {
                    CorrelationId = index,
                    Limit = 0
                });

            case WriteModelType.DeleteOne:
                var deleteOneModel = (DeleteOneModel <TDocument>)model;
                return(new DeleteRequest(deleteOneModel.Filter.Render(_documentSerializer, _settings.SerializerRegistry))
                {
                    CorrelationId = index,
                    Limit = 1
                });

            case WriteModelType.ReplaceOne:
                var replaceOneModel = (ReplaceOneModel <TDocument>)model;
                return(new UpdateRequest(
                           UpdateType.Replacement,
                           replaceOneModel.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
                           new BsonDocumentWrapper(replaceOneModel.Replacement, _documentSerializer))
                {
                    CorrelationId = index,
                    IsMulti = false,
                    IsUpsert = replaceOneModel.IsUpsert
                });

            case WriteModelType.UpdateMany:
                var updateManyModel = (UpdateManyModel <TDocument>)model;
                return(new UpdateRequest(
                           UpdateType.Update,
                           updateManyModel.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
                           updateManyModel.Update.Render(_documentSerializer, _settings.SerializerRegistry))
                {
                    CorrelationId = index,
                    IsMulti = true,
                    IsUpsert = updateManyModel.IsUpsert
                });

            case WriteModelType.UpdateOne:
                var updateOneModel = (UpdateOneModel <TDocument>)model;
                return(new UpdateRequest(
                           UpdateType.Update,
                           updateOneModel.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
                           updateOneModel.Update.Render(_documentSerializer, _settings.SerializerRegistry))
                {
                    CorrelationId = index,
                    IsMulti = false,
                    IsUpsert = updateOneModel.IsUpsert
                });

            default:
                throw new InvalidOperationException("Unknown type of WriteModel provided.");
            }
        }
        public async Task BulkWriteAsync_should_execute_the_BulkMixedWriteOperation(bool isOrdered)
        {
            var requests = new WriteModel <BsonDocument>[]
            {
                new InsertOneModel <BsonDocument>(new BsonDocument("_id", 1).Add("a", 1)),
                new DeleteManyModel <BsonDocument>(new BsonDocument("b", 1)),
                new DeleteOneModel <BsonDocument>(new BsonDocument("c", 1)),
                new ReplaceOneModel <BsonDocument>(new BsonDocument("d", 1), new BsonDocument("e", 1)),
                new ReplaceOneModel <BsonDocument>(new BsonDocument("f", 1), new BsonDocument("g", 1))
                {
                    IsUpsert = true
                },
                new UpdateManyModel <BsonDocument>(new BsonDocument("h", 1), new BsonDocument("$set", new BsonDocument("i", 1))),
                new UpdateManyModel <BsonDocument>(new BsonDocument("j", 1), new BsonDocument("$set", new BsonDocument("k", 1)))
                {
                    IsUpsert = true
                },
                new UpdateOneModel <BsonDocument>(new BsonDocument("l", 1), new BsonDocument("$set", new BsonDocument("m", 1))),
                new UpdateOneModel <BsonDocument>(new BsonDocument("n", 1), new BsonDocument("$set", new BsonDocument("o", 1)))
                {
                    IsUpsert = true
                },
            };
            var bulkOptions = new BulkWriteOptions
            {
                IsOrdered = isOrdered
            };

            var operationResult = new BulkWriteOperationResult.Unacknowledged(9, new[] { new InsertRequest(new BsonDocument("b", 1)) });

            _operationExecutor.EnqueueResult <BulkWriteOperationResult>(operationResult);

            var result = await _subject.BulkWriteAsync(requests, bulkOptions, CancellationToken.None);

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

            call.Operation.Should().BeOfType <BulkMixedWriteOperation>();
            var operation = (BulkMixedWriteOperation)call.Operation;

            // I know, this is a lot of stuff in one test :(
            operation.CollectionNamespace.FullName.Should().Be("foo.bar");
            operation.IsOrdered.Should().Be(isOrdered);
            operation.Requests.Count().Should().Be(9);
            var convertedRequests = operation.Requests.ToList();

            // InsertOneModel
            convertedRequests[0].Should().BeOfType <InsertRequest>();
            convertedRequests[0].CorrelationId.Should().Be(0);
            var convertedRequest0 = (InsertRequest)convertedRequests[0];

            convertedRequest0.Document.Should().Be("{_id:1, a:1}");

            // RemoveManyModel
            convertedRequests[1].Should().BeOfType <DeleteRequest>();
            convertedRequests[1].CorrelationId.Should().Be(1);
            var convertedRequest1 = (DeleteRequest)convertedRequests[1];

            convertedRequest1.Filter.Should().Be("{b:1}");
            convertedRequest1.Limit.Should().Be(0);

            // RemoveOneModel
            convertedRequests[2].Should().BeOfType <DeleteRequest>();
            convertedRequests[2].CorrelationId.Should().Be(2);
            var convertedRequest2 = (DeleteRequest)convertedRequests[2];

            convertedRequest2.Filter.Should().Be("{c:1}");
            convertedRequest2.Limit.Should().Be(1);

            // ReplaceOneModel
            convertedRequests[3].Should().BeOfType <UpdateRequest>();
            convertedRequests[3].CorrelationId.Should().Be(3);
            var convertedRequest3 = (UpdateRequest)convertedRequests[3];

            convertedRequest3.Filter.Should().Be("{d:1}");
            convertedRequest3.Update.Should().Be("{e:1}");
            convertedRequest3.UpdateType.Should().Be(UpdateType.Replacement);
            convertedRequest3.IsMulti.Should().BeFalse();
            convertedRequest3.IsUpsert.Should().BeFalse();

            // ReplaceOneModel with upsert
            convertedRequests[4].Should().BeOfType <UpdateRequest>();
            convertedRequests[4].CorrelationId.Should().Be(4);
            var convertedRequest4 = (UpdateRequest)convertedRequests[4];

            convertedRequest4.Filter.Should().Be("{f:1}");
            convertedRequest4.Update.Should().Be("{g:1}");
            convertedRequest4.UpdateType.Should().Be(UpdateType.Replacement);
            convertedRequest4.IsMulti.Should().BeFalse();
            convertedRequest4.IsUpsert.Should().BeTrue();

            // UpdateManyModel
            convertedRequests[5].Should().BeOfType <UpdateRequest>();
            convertedRequests[5].CorrelationId.Should().Be(5);
            var convertedRequest5 = (UpdateRequest)convertedRequests[5];

            convertedRequest5.Filter.Should().Be("{h:1}");
            convertedRequest5.Update.Should().Be("{$set:{i:1}}");
            convertedRequest5.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest5.IsMulti.Should().BeTrue();
            convertedRequest5.IsUpsert.Should().BeFalse();

            // UpdateManyModel with upsert
            convertedRequests[6].Should().BeOfType <UpdateRequest>();
            convertedRequests[6].CorrelationId.Should().Be(6);
            var convertedRequest6 = (UpdateRequest)convertedRequests[6];

            convertedRequest6.Filter.Should().Be("{j:1}");
            convertedRequest6.Update.Should().Be("{$set:{k:1}}");
            convertedRequest6.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest6.IsMulti.Should().BeTrue();
            convertedRequest6.IsUpsert.Should().BeTrue();

            // UpdateOneModel
            convertedRequests[7].Should().BeOfType <UpdateRequest>();
            convertedRequests[7].CorrelationId.Should().Be(7);
            var convertedRequest7 = (UpdateRequest)convertedRequests[7];

            convertedRequest7.Filter.Should().Be("{l:1}");
            convertedRequest7.Update.Should().Be("{$set:{m:1}}");
            convertedRequest7.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest7.IsMulti.Should().BeFalse();
            convertedRequest7.IsUpsert.Should().BeFalse();

            // UpdateOneModel with upsert
            convertedRequests[8].Should().BeOfType <UpdateRequest>();
            convertedRequests[8].CorrelationId.Should().Be(8);
            var convertedRequest8 = (UpdateRequest)convertedRequests[8];

            convertedRequest8.Filter.Should().Be("{n:1}");
            convertedRequest8.Update.Should().Be("{$set:{o:1}}");
            convertedRequest8.UpdateType.Should().Be(UpdateType.Update);
            convertedRequest8.IsMulti.Should().BeFalse();
            convertedRequest8.IsUpsert.Should().BeTrue();

            // Result
            result.Should().NotBeNull();
            result.IsAcknowledged.Should().BeFalse();
            result.RequestCount.Should().Be(9);
            result.ProcessedRequests.Should().BeEquivalentTo(requests);
            for (int i = 0; i < requests.Length; i++)
            {
                result.ProcessedRequests[i].Should().BeSameAs(requests[i]);
            }
        }
Beispiel #4
0
        private WriteRequest ConvertWriteModelToWriteRequest(WriteModel <T> model, int index)
        {
            switch (model.ModelType)
            {
            case WriteModelType.InsertOne:
                var insertOneModel = (InsertOneModel <T>)model;
                AssignId(insertOneModel.Document);
                return(new InsertRequest(new BsonDocumentWrapper(insertOneModel.Document, _serializer))
                {
                    CorrelationId = index
                });

            case WriteModelType.RemoveMany:
                var removeManyModel = (RemoveManyModel <T>)model;
                return(new DeleteRequest(ConvertToBsonDocument(removeManyModel.Criteria))
                {
                    CorrelationId = index,
                    Limit = 0
                });

            case WriteModelType.RemoveOne:
                var removeOneModel = (RemoveOneModel <T>)model;
                return(new DeleteRequest(ConvertToBsonDocument(removeOneModel.Criteria))
                {
                    CorrelationId = index,
                    Limit = 1
                });

            case WriteModelType.ReplaceOne:
                var replaceOneModel = (ReplaceOneModel <T>)model;
                return(new UpdateRequest(
                           ConvertToBsonDocument(replaceOneModel.Criteria),
                           new BsonDocumentWrapper(replaceOneModel.Replacement, _serializer))
                {
                    CorrelationId = index,
                    IsMultiUpdate = false,
                    IsUpsert = replaceOneModel.IsUpsert
                });

            case WriteModelType.UpdateMany:
                var updateManyModel = (UpdateManyModel <T>)model;
                return(new UpdateRequest(
                           ConvertToBsonDocument(updateManyModel.Criteria),
                           ConvertToBsonDocument(updateManyModel.Update))
                {
                    CorrelationId = index,
                    IsMultiUpdate = true,
                    IsUpsert = updateManyModel.IsUpsert
                });

            case WriteModelType.UpdateOne:
                var updateOneModel = (UpdateOneModel <T>)model;
                return(new UpdateRequest(
                           ConvertToBsonDocument(updateOneModel.Criteria),
                           ConvertToBsonDocument(updateOneModel.Update))
                {
                    CorrelationId = index,
                    IsMultiUpdate = false,
                    IsUpsert = updateOneModel.IsUpsert
                });

            default:
                throw new InvalidOperationException("Unknown type of WriteModel provided.");
            }
        }