8

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.

1

3 Answers 3

10

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!

Sign up to request clarification or add additional context in comments.

3 Comments

I looked at this exact same page and could've sworn I was passing the same thing. Must just be in a Friday end of the week haze.
It's happen with all devs!
In the latest version of the driver it's now PipelineDefinition<BsonDocument /* or your collection type */, BsonDocument> pipeline = new BsonDocument[] { }
9

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.

Comments

1

thanks Filipe, while searching for the same issue I have find out that from MongoDB Driver 2.11 the PipelineDefinition syntax has changed requiring TInput and TOutput generics:

PipelineDefinition<BsonDocument, BsonDocument> pipeline = new 
BsonDocument[]
{
    new BsonDocument { { "$match", new BsonDocument("x", 1) } },
    new BsonDocument { { "$sort", new BsonDocument("y", 1) } }
};

The most recent docs is here

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.