public async Task MapReduceAsync_with_collection_output_mode_should_execute_the_MapReduceOperation()
        {
            var filter  = new BsonDocument("filter", 1);
            var scope   = new BsonDocument("test", 3);
            var sort    = new BsonDocument("sort", 1);
            var options = new MapReduceOptions <BsonDocument>
            {
                Filter         = new BsonDocument("filter", 1),
                Finalize       = "finalizer",
                JavaScriptMode = true,
                Limit          = 10,
                MaxTime        = TimeSpan.FromMinutes(2),
                OutputOptions  = MapReduceOutputOptions.Replace("awesome", "otherDB", true),
                Scope          = scope,
                Sort           = sort,
                Verbose        = true
            };
            var result = await _subject.MapReduceAsync("map", "reduce", options);

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

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

            operation.CollectionNamespace.FullName.Should().Be("foo.bar");

            operation.Filter.Should().Be(filter);
            operation.FinalizeFunction.Should().Be(options.Finalize);
            operation.JavaScriptMode.Should().Be(options.JavaScriptMode);
            operation.Limit.Should().Be(options.Limit);
            operation.MapFunction.Should().Be(new BsonJavaScript("map"));
            operation.MaxTime.Should().Be(options.MaxTime);
            operation.NonAtomicOutput.Should().NotHaveValue();
            operation.OutputCollectionNamespace.Should().Be(CollectionNamespace.FromFullName("otherDB.awesome"));
            operation.OutputMode.Should().Be(Core.Operations.MapReduceOutputMode.Replace);
            operation.ReduceFunction.Should().Be(new BsonJavaScript("reduce"));
            operation.Scope.Should().Be(scope);
            operation.ShardedOutput.Should().Be(true);
            operation.Sort.Should().Be(sort);
            operation.Verbose.Should().Be(options.Verbose);
        }
        private MapReduceOutputToCollectionOperation CreateMapReduceOutputToCollectionOperation <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options, MapReduceOutputOptions outputOptions)
        {
            var collectionOutputOptions = (MapReduceOutputOptions.CollectionOutput)outputOptions;
            var databaseNamespace       = collectionOutputOptions.DatabaseName == null ?
                                          _collectionNamespace.DatabaseNamespace :
                                          new DatabaseNamespace(collectionOutputOptions.DatabaseName);
            var outputCollectionNamespace = new CollectionNamespace(databaseNamespace, collectionOutputOptions.CollectionName);

            return(new MapReduceOutputToCollectionOperation(
                       _collectionNamespace,
                       outputCollectionNamespace,
                       map,
                       reduce,
                       _messageEncoderSettings)
            {
                BypassDocumentValidation = options.BypassDocumentValidation,
                Collation = options.Collation,
                Filter = options.Filter == null ? null : options.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
                FinalizeFunction = options.Finalize,
                JavaScriptMode = options.JavaScriptMode,
                Limit = options.Limit,
                MaxTime = options.MaxTime,
                NonAtomicOutput = collectionOutputOptions.NonAtomic,
                Scope = options.Scope,
                OutputMode = collectionOutputOptions.OutputMode,
                ShardedOutput = collectionOutputOptions.Sharded,
                Sort = options.Sort == null ? null : options.Sort.Render(_documentSerializer, _settings.SerializerRegistry),
                Verbose = options.Verbose,
                WriteConcern = _settings.WriteConcern
            });
        }