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.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With