Q:

C# MongoDb running an aggregate query directly from JSON

I'm working on a service method that can run mongodb aggregate queries from a json input. The idea is that you would use Builders to generate a query, convert that query to json, pass it to the service to be deserialized and run. For find queries I was able to use Bson documents like so
    public string DoGenericFind(string queryDoc, string collectionName)
    {
        BsonDocument document = BsonSerializer.Deserialize<BsonDocument>(queryDoc);

        var results = _context.Database.GetCollection<dynamic>(collectionName).FindSync<BsonDocument>(document);

        if (results == null)
            return null;
        else 
            return results.ToList().ToJson();
    }

I'm having trouble finding a similar way to do this with aggregate. The only examples I'm finding around try to do something similar to this where they pass some kind of BsonDocument[]. However, the intellisense for my version of the driver(2.5) say that I need to pass a pipelineDefinition which I can't find good examples of how to use.

A:

Found a good example right after posting this. https://groups.google.com/forum/#!topic/mongodb-user/Otg17LUE_7M
Which made my final solution look like 
    public string DoGenericAggregate(string queryDoc, string collectionName)
    {
        var query = BsonSerializer.Deserialize<BsonDocument[]>(queryDoc).ToList();

        List<BsonDocument> list;
        using (var cursor = _context.Database.GetCollection<dynamic>(collectionName).Aggregate<BsonDocument>(query))
        {
            list = cursor.ToList();
        }

        if (list == null)
            return null;
        else
            return list.ToJson();
    }

Was pretty sure I had something crazy similar to this but was getting errors. But anyways, a List<BsonDocument> is implicitly convertible to PipelineDefinition<,> so that is passable to the aggregate function.

A:

Look at this MongoDB Driver docs page: 
Definitions and Builders
In the topic PIPELINE has a simple example that can help you.
You will need create a simple code like that to use the pipeline definition.
PipelineDefinition pipeline = new BsonDocument[] 
{
    new BsonDocument { { "$match", new BsonDocument("x", 1) } },
    new BsonDocument { { "$sort", new BsonDocument("y", 1) } }
};

col.aggregate(pipeline);

Hope this helps!

