在c#中执行聚合框架查询

本文关键字:框架 查询 执行 | 更新日期: 2023-09-27 17:58:25

我正在尝试创建一个控制台应用程序,该应用程序将读取Mongo数据库,并根据一些业务逻辑过滤出一些数据,并在控制台屏幕上显示结果。

到目前为止,我已经能够编写以下查询并在Mongo-shell上成功执行。以下是查询:

{db.Collection.aggregate([
{$unwind: { path: "$Pages"}},
{$group : {
  _id :{UrlPath: "$Pages.Url.Path", I_Id : "$_id", Date: { $dateToString: { format: "%Y-%m-%d",date: "$Pages.DateTime"}}, CId: "$CId",
   x: {$sum:1},
   y : {$sum:"$V"}
 }},     
 {$group : {
   _id : {Date: "$_id.Date",CId: "$_id.CId", PageUrl: "$_id.UrlPath"},
    p: {$sum:1},
    q : {$sum:"$x"},
    TotalCount: {$sum:"$y"}
 }},
 {$sort:{p:-1}},     
],{allowDiskUse: true}).pretty();
};

我面临的问题是在C#代码中复制相同的查询逻辑。到目前为止,我能够连接到mongodb集合,并能够进行基本的CRUD操作。

但复制这种聚合逻辑真的让我大吃一惊。我尝试过使用PipelineDefinition选项,但无法获得正确的输出

有人能在这里给我指引正确的方向吗。

提前感谢

在c#中执行聚合框架查询

最后我能够使用collection实现它。Aggregate()方法。

代码如下:

collection.Aggregate(new AggregateOptions { AllowDiskUse = true})
                        .Unwind(i => i.Pages)
                        .Group(new BsonDocument
                                        {
                                               {
                                                   "_id", new BsonDocument 
                                                        {
                                                            { "Date", new BsonDocument("$dateToString", new BsonDocument {{"format", "%Y-%m-%d"}, {"date", "$Pages.DateTime"}})},                                                        
                                                            {"CId","$_id"}                                                                
                                                        }
                                               },
                                               {
                                                   "x", new BsonDocument
                                                        {
                                                            {"$sum", "$Value"}
                                                        }
                                               } 
                                        }
                                )
                          .Group(new BsonDocument
                                        {
                                               {
                                                   "_id", new BsonDocument 
                                                        {
                                                            {"Date", "$_id.Date"},
                                                            {"CId", "$_id.CId"}                                                           
                                                        }
                                               },
                                               {
                                                   "p", new BsonDocument
                                                        {
                                                            {"$sum", 1}
                                                        }                                                      
                                               },
                                               {
                                                   "q", new BsonDocument
                                                        {
                                                            {"$sum", "$x"}
                                                        }
                                               }
                                        }
                                 )
                            .Sort(new BsonDocument("q", -1))
                            .ToList();