public void Merge_with_default_options_should_return_the_expected_result(string outputDatabaseName, string outputCollectionName, string expectedStage) { var client = DriverTestConfiguration.Client; var outputDatabase = client.GetDatabase(outputDatabaseName); var outputCollection = outputDatabase.GetCollection <BsonDocument>(outputCollectionName); var mergeOptions = new MergeStageOptions <BsonDocument>(); var result = PipelineStageDefinitionBuilder.Merge <BsonDocument, BsonDocument>(outputCollection, mergeOptions); var stage = RenderStage(result); stage.Document.Should().Be(expectedStage); }
public void Aggregate_with_write_stage_should_not_have_transaction_id( [Values("$out", "$merge")] string outStage, [Values(false, true)] bool async) { RequireServer .Check() .ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded) .Serverless(false); // $out and $merge are not supported on serverless. if (outStage == "$merge") { RequireServer.Check().Supports(Feature.AggregateMerge); } DropAndCreateCollection(); var eventCapturer = CreateEventCapturer(); using (var client = CreateDisposableClient(eventCapturer)) { var database = client.GetDatabase(_databaseName); var collection = database.GetCollection <BsonDocument>(_collectionName); PipelineDefinition <BsonDocument, BsonDocument> pipeline = new EmptyPipelineDefinition <BsonDocument>(); var outputCollection = database.GetCollection <BsonDocument>(_collectionName + "-outputCollection"); switch (outStage) { case "$out": pipeline = pipeline.Out(outputCollection); break; case "$merge": var mergeOptions = new MergeStageOptions <BsonDocument>(); pipeline = pipeline.Merge(outputCollection, mergeOptions); break; default: throw new Exception($"Unexpected outStage: {outStage}."); } if (async) { collection.AggregateAsync(pipeline).GetAwaiter().GetResult(); } else { collection.Aggregate(pipeline); } AssertCommandDoesNotHaveTransactionId(eventCapturer); } }
public void Merge_should_add_expected_stage() { var pipeline = new EmptyPipelineDefinition <BsonDocument>(); var client = DriverTestConfiguration.Client; var outputDatabase = client.GetDatabase("database"); var outputCollection = outputDatabase.GetCollection <BsonDocument>("collection"); var mergeOptions = new MergeStageOptions <BsonDocument>(); var result = pipeline.Merge(outputCollection, mergeOptions); var stages = RenderStages(result, BsonDocumentSerializer.Instance); stages.Count.Should().Be(1); stages[0].Should().Be("{ $merge : { into : { db : 'database', coll : 'collection' } } }"); }
public void Merge_with_WhenNotMatched_should_return_the_expected_result(MergeStageWhenNotMatched whenNotMatched, string expectedStage) { var client = DriverTestConfiguration.Client; var outputDatabase = client.GetDatabase("database"); var outputCollection = outputDatabase.GetCollection <BsonDocument>("collection"); var mergeOptions = new MergeStageOptions <BsonDocument> { WhenNotMatched = whenNotMatched }; var result = PipelineStageDefinitionBuilder.Merge <BsonDocument, BsonDocument>(outputCollection, mergeOptions); var stage = RenderStage(result); stage.Document.Should().Be(expectedStage); }
public void Merge_with_OnFieldNames_should_return_the_expected_result(string fieldNames, string expectedStage) { var client = DriverTestConfiguration.Client; var outputDatabase = client.GetDatabase("database"); var outputCollection = outputDatabase.GetCollection <BsonDocument>("collection"); var mergeOptions = new MergeStageOptions <BsonDocument> { OnFieldNames = fieldNames.Split(',') }; var result = PipelineStageDefinitionBuilder.Merge <BsonDocument, BsonDocument>(outputCollection, mergeOptions); var stage = RenderStage(result); stage.Document.Should().Be(expectedStage); }
public void Merge_should_add_the_expected_stage_and_call_Aggregate( [Values(false, true)] bool async) { var subject = CreateCollectionSubject() .Match(Builders <C> .Filter.Eq(c => c.X, 1)); var outputDatabase = subject.Database; var outputCollection = outputDatabase.GetCollection <C>("collection"); var mergeOptions = new MergeStageOptions <C>(); Predicate <PipelineDefinition <C, C> > isExpectedPipeline = pipeline => { var renderedPipeline = RenderPipeline(pipeline); return (renderedPipeline.Documents.Count == 2 && renderedPipeline.Documents[0] == BsonDocument.Parse("{ $match : { X : 1 } }") && renderedPipeline.Documents[1] == BsonDocument.Parse("{ $merge : { into : { db : 'test', coll : 'collection' } } }") && renderedPipeline.OutputSerializer.ValueType == typeof(C)); }; IAsyncCursor <C> cursor; if (async) { cursor = subject.MergeAsync(outputCollection, mergeOptions, CancellationToken.None).GetAwaiter().GetResult(); _mockCollection.Verify( c => c.AggregateAsync <C>( It.Is <PipelineDefinition <C, C> >(pipeline => isExpectedPipeline(pipeline)), It.IsAny <AggregateOptions>(), CancellationToken.None), Times.Once); } else { cursor = subject.Merge(outputCollection, mergeOptions, CancellationToken.None); _mockCollection.Verify( c => c.Aggregate <C>( It.Is <PipelineDefinition <C, C> >(pipeline => isExpectedPipeline(pipeline)), It.IsAny <AggregateOptions>(), CancellationToken.None), Times.Once); } }
public void Merge_with_LetVariables_should_return_the_expected_result(string letVariables, string expectedStage) { var client = DriverTestConfiguration.Client; var outputDatabase = client.GetDatabase("database"); var outputCollection = outputDatabase.GetCollection <BsonDocument>("collection"); var mergeOptions = new MergeStageOptions <BsonDocument> { LetVariables = BsonDocument.Parse(letVariables), WhenMatched = MergeStageWhenMatched.Pipeline, WhenMatchedPipeline = new EmptyPipelineDefinition <BsonDocument>().Project("{ _id : '$_id' }") }; var result = PipelineStageDefinitionBuilder.Merge <BsonDocument, BsonDocument>(outputCollection, mergeOptions); var stage = RenderStage(result); stage.Document.Should().Be(expectedStage); }
public Task <IAsyncCursor <TOutput> > MergeAsync <TOutput>(IMongoCollection <TOutput> outputCollection, MergeStageOptions <TOutput> mergeOptions = null, CancellationToken cancellationToken = default) { return(BlockProvider.MergeAsync <TOutput>(outputCollection, mergeOptions, cancellationToken)); }