Mongodb在CSharp里實現(xiàn)Aggregate實例
今天主要用了一個mongodb.driver里的分組,事實上在網(wǎng)上介紹這方面的文章非常少,以至于我在出現(xiàn)問題后,無法找到一個正確的解決方案,最后還是通過異常信息找到的解決方法,所以感覺自己更應(yīng)該去寫一篇關(guān)于如何在C#驅(qū)動里進行聚合Aggregate的文章!
/// <summary>
    /// 返回UI消息樹
    /// </summary>
    /// <returns></returns>
    public static string GetMongoLog(DateTime? fromDate, DateTime? toDate, int page = 1)
    {
      string from = DateTime.Now.Date.ToString("yyyy-MM-dd");
      string to = DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd");
      if (fromDate.HasValue)
      {
        from = fromDate.Value.ToString("yyyy-MM-dd");
      }
      if (toDate.HasValue)
      {
        to = toDate.Value.ToString("yyyy-MM-dd");
      }
      var stages = new List<IPipelineStageDefinition>();
      stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$match:{AddTime:{$gt:ISODate('" + from + "'),$lt:ISODate('" + to + "')}}}"));
      stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$group:{_id: \"$RootId\", count: {$sum: 1}}}"));
      stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$skip:" + page * 5 + "}"));
      stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>("{$limit:5}"));
      var pipeline = new PipelineStagePipelineDefinition<BsonDocument, BsonDocument>(stages);
      var result = NoSql.MongodbManager<LoggerContext>.Collection.Aggregate(pipeline);
      StringBuilder str = new StringBuilder();
      str.Append("<ol class='treeMsg'>");
      foreach (var item in result.ToList())
      {
        var timer = new List<DateTime>();
        var old = NoSql.MongodbManager<LoggerContext>.Instance.Find(i => i.RootId == item.Values.ToArray()[0].ToString() && i.ParentId == null).FirstOrDefault();
        timer.Add(old.AddTime);
        str.Append("<li style='margin:5px;border:1px dashed #aaa'>");
        str.AppendFormat("<span style='color:red;'>{0}</span><span style='color:green'>{1}</span><span>{2}</span>"
          , old.Url
          , old.MessageBody
          , old.AddTime);
        MsgTree(str, old.ChildId, timer);
        str.AppendFormat("<p><em>本次請求用時{0}毫秒({1}秒)<em></p>"
          , (timer.Max() - timer.Min()).TotalMilliseconds
          , (timer.Max() - timer.Min()).TotalSeconds);
        str.Append("</li>");
      }
      str.Append("</ol>");
      return str.ToString();
    }
注意,目前mongodb for C#這個驅(qū)動,在進行Aggregate時,只支持BsonDocument類型,也就是說,你的集合collection也必須返回的是BsonDocument,而實體類型是不可以被認出的,這點要注意.
也正是如此,所以我們的mongo封裝時,別忘記公開一個BsonDocument的對象供聚合使用!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
本文標題:Mongodb在CSharp里實現(xiàn)Aggregate實例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5702.html
您可能感興趣的文章
- 01-10C#實現(xiàn)將窗體固定在顯示器的左上角且不能移動的方法
 - 01-10C#實現(xiàn)在Form里面內(nèi)嵌dos窗體的方法
 - 01-10C#中查找Dictionary中的重復(fù)值的方法
 - 01-10C#實現(xiàn)在啟動目錄創(chuàng)建快捷方式的方法
 - 01-10關(guān)于nancy中的身份驗證
 - 01-10C#編程自學(xué)之類和對象
 - 01-10C#創(chuàng)建不規(guī)則窗體的4種方式詳解
 - 01-10C#實現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
 - 01-10C#中yield用法使用說明
 - 01-10C#編程自學(xué)之流程控制語句
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
 
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10C#中split用法實例總結(jié)
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 04-02jquery與jsp,用jquery
 - 01-10delphi制作wav文件的方法
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 


