Re: [mongodb-user] how to query it in C#?

93 views
Skip to first unread message

Robert Stam

unread,
Oct 10, 2012, 6:44:08 PM10/10/12
to mongod...@googlegroups.com
You can use the new Aggregate helper method that was introduced in version 1.6 of the C# driver. The hard part is settings up the array of aggregation framework operations.

Your example would look like this in C#:

var operations = new BsonDocument[]
{
    new BsonDocument("$match", new BsonDocument("words", new BsonDocument("$all", new BsonArray { "XXS", "SMT" }))),
    new BsonDocument("$project", new BsonDocument { { "_id", 1 }, { "words", 1 } }),
    new BsonDocument("$unwind", "$words"),
    new BsonDocument("$group", new BsonDocument { { "_id", new BsonDocument("tags", "$words") }, { "count", new BsonDocument("$sum", 1) } }),
    new BsonDocument("$sort", new BsonDocument("count", 1)),
    new BsonDocument("$limit", 5)
};
var result = collection.Aggregate(operations);
foreach (var document in result.ResultDocuments)
{
    // process result document
}

On Sat, Oct 6, 2012 at 2:08 PM, Bohdan Cherchyk <cher...@gmail.com> wrote:

Hi

I have a query

db.foo.aggregate(
   { $match: {words: {$all: ["XXS", "SMT"]}}},
   { $project : {_id : 1, words : 1,}},
   { $unwind : "$words" },
   { $group : { _id : { tags : "$words" }, count : { $sum : 1 }} },
   {$sort: {count:-1}},
   { $limit : 5 }
);


what is the right approach to run it in C# ?

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Reply all
Reply to author
Forward
0 new messages