【问题标题】:Executing Aggregate framework query in c#在 C# 中执行聚合框架查询
【发布时间】:2016-07-25 06:42:17
【问题描述】:

我正在尝试创建一个控制台应用程序,它将通过 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# 代码中复制相同的查询逻辑。到目前为止,我能够连接到 mongo db 集合并能够进行基本的 CRUD 操作。

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

有人可以在这里指导我正确的方向吗?

提前致谢

【问题讨论】:

    标签: c# mongodb aggregation-framework


    【解决方案1】:

    最后我能够使用 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2014-05-11
      • 2018-11-11
      • 2022-12-05
      • 2013-01-03
      • 1970-01-01
      相关资源
      最近更新 更多