Reputation: 5767
I am trying to use the aggregate function to get a count of popular tags in a certain collection. I want to simply execute pure MongoDB queries via MongoDb for Windows & .Net(C# 4.0) driver.
Here is the mongo shell query, I wish to execute (see Mongodb aggregate, How to count documents by interval criteria?)?
collection.aggregate({
$group: { "_id": { $cond: [{$gte: ["$LoadTime", 2000]}, "Slowest",
{$cond: [ {$and: [{$lt: ["$LoadTime", 2000] },{$gte: ["$LoadTime", 1000]}]}, "Slow",
{$cond: [{$and: [{$lt: ["$LoadTime", 1000]}, {$gte: ["$LoadTime", 500 ]} ]},"Medium","Fast"]}
]}
]},
"count": {$sum: 1}
}
})
I am not sure how can i do this. Perhaps with call Database.RunCommand("TheCommand") or bson_xxxx methods but I don't know how. Is it possible to execute pure MongoDB queries via .Net (C# 4.0) driver?
Upvotes: 0
Views: 1322
Reputation: 731
For aggregation, use MongoCollection's IEnumerable Aggregate(AggregateArgs args). FYI this is the version in 1.9rc, the prior overloads of Aggregate have been deprecated in favor of using the new AggregateArgs.
Aggregate doesn't have fluent building support in the c# driver (yet), so you're basically creating BsonDocument instances for each stage in your pipeline and passing that array of documents to AggregateArgs.Pipeline.
Under the "hood" this is using CommandDocument, but I don't see much benefit to use that directly when the driver already takes care of that via MongoCollection's internal RunAggregateCommand.
Upvotes: 0