locked
Using Aggregate<> function in IMongoCollection and how it is different form .Aggregate() in MongoCollection RRS feed

  • Question

  • I am pretty new to the Mongo world.

    I am trying to execute a aggregate function in c# using pipeline approach. Here's the code

    MongoClient client = new MongoClient("mongodb://localhost:27017");

    IMongoDatabase database = client.GetDatabase("Interaction");

    IMongoCollection<CollectionStructure.Interactions> collection = database.GetCollection <CollectionStructure.Interactions>("Interactions");
    //IMongoCollection<CollectionStructure.Interactions> result;

    var unwind = new BsonDocument
    {
        {
            "$unwind",
            new BsonDocument
            {
                {"path", "$Pages" }
            }
        }
    };

    var group1 = new BsonDocument
    {
        {
            "$group",
            new BsonDocument
            {
                {
                    "_id", new BsonDocument
                    {
                        {"UrlPath", "$Pages.Url.Path"},
                        {"InteractionId", "$_id"}
                    }
                },
                {
                    "count", new BsonDocument
                    {
                        {"$sum", 1}
                    }
                }
            }
        }

    };
    var group2 = new BsonDocument
    {
        {
            "$group",
            new BsonDocument
            {
                {
                    "_id", new BsonDocument
                    {
                        {"UrlPath", "$_id.UrlPath"}
                    }
                },
                {
                    "distinctCount", new BsonDocument
                    {
                        {"$sum", 1}
                    }
                }
            }
        }

    };

    var sort = new BsonDocument
    {
        {
            "$sort",
            new BsonDocument
            {
                {"distinctCount", "-1" }
            }
        }
    };

    AggregateArgs pipeline = new AggregateArgs(); //= new[] {unwind,group1,group2,sort};
    pipeline.Pipeline = new[] { unwind, group1, group2, sort };


    ##error##
    var result = collection.Aggregate<>(pipeline);

    So 2 quick questions:

    1. How exactly to use Aggregate function in above scenario. Please guide me if I am doing something wrong
    2. What are these 2 separate collection MongoCollection and IMongoCollections and when to use what
    Please help me in this.

    Thanks in advance

    I am pretty new to the Mongo world. I am trying to execute a aggregate function in c# using pipeline approach. Here's the code

    MongoClient client = new MongoClient("mongodb://localhost:27017");
    
    IMongoDatabase database = client.GetDatabase("Interaction");
    
    IMongoCollection<CollectionStructure.Interactions> collection = database.GetCollection <CollectionStructure.Interactions>("Interactions");
    //IMongoCollection<CollectionStructure.Interactions> result;
    
    var unwind = new BsonDocument
    {
     {
     "$unwind",
     new BsonDocument
     {
     {"path", "$Pages" }
     }
     }
    };
    
    var group1 = new BsonDocument
    {
     {
     "$group",
     new BsonDocument
     {
     {
     "_id", new BsonDocument
     {
     {"UrlPath", "$Pages.Url.Path"},
     {"InteractionId", "$_id"}
     }
     },
     {
     "count", new BsonDocument
     {
     {"$sum", 1}
     }
     }
     }
     }
    
    };
    var group2 = new BsonDocument
    {
     {
     "$group",
     new BsonDocument
     {
     {
     "_id", new BsonDocument
     {
     {"UrlPath", "$_id.UrlPath"}
     }
     },
     {
     "distinctCount", new BsonDocument
     {
     {"$sum", 1}
     }
     }
     }
     }
    
    };
    
    var sort = new BsonDocument
    {
     {
     "$sort",
     new BsonDocument
     {
     {"distinctCount", "-1" }
     }
     }
    };
    
    AggregateArgs pipeline = new AggregateArgs(); //= new[] {unwind,group1,group2,sort};
    pipeline.Pipeline = new[] { unwind, group1, group2, sort };
    
    
    ##error##
    var result = collection.Aggregate<>(pipeline);

    So 2 quick questions:

    1. How exactly to use Aggregate function in above scenario. Please guide me if I am doing something wrong

    2. What are these 2 separate collection MongoCollection and IMongoCollections and when to use what

    Please help me in this. Thanks in advance

    • Moved by CoolDadTx Thursday, July 21, 2016 2:11 PM Third party product
    Thursday, July 21, 2016 7:19 AM

All replies

  • These forums are for MS products only. Questions related to Mongo and how you use it need to be posted in their forums.
    Thursday, July 21, 2016 2:11 PM