public async Task <IActionResult> Aggregate(string clusterId, string dbName, string collectionName,
                                                    [FromBody] MongoAggregateRequest body)
        {
            var aggregateCursor = await AggregateInternal(clusterId, dbName, collectionName, body);

            var aggregate = await aggregateCursor.ToListAsync();

            var result = aggregate.Select(a => a.ToJObject()).ToList();

            return(Ok(result));
        }
        private async Task <IAsyncCursor <BsonDocument> > AggregateInternal(
            string clusterId, string dbName, string collectionName, MongoAggregateRequest body)
        {
            var collection = _configuration.GetCollection(clusterId, dbName, collectionName);
            var stages     = body.Stages?.Select(s => s.ToBsonDocument().Preprocess());

            var pipelineDefinition = PipelineDefinition <BsonDocument, BsonDocument> .Create(stages);

            var aggregateOptions = new AggregateOptions {
            };

            return(await collection.AggregateAsync(pipelineDefinition, aggregateOptions));
        }
        public async Task <IActionResult> AggregateToObject(string clusterId, string dbName, string collectionName,
                                                            [FromBody] MongoAggregateRequest body)
        {
            var aggregateCursor = await AggregateInternal(clusterId, dbName, collectionName, body);

            var resultBson = await aggregateCursor.SingleOrDefaultAsync();

            if (resultBson == null)
            {
                return(NotFound());
            }

            return(Ok(resultBson.ToJObject()));
        }