在C#中访问MongoDB,查询与修改

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Globalization;
 using MongoDB.Driver;
 using MongoDB.Driver.Linq;
 using MongoDB.Bson;
 using MongoDB.Driver.Builders;
 using Com.iFlytek.MarkerPlatform.DomainObjects;
 
 namespace Com.iFlytek.MarkerPlatform.Helper
 {
     public class MongdbHelper : IDisposable
     {
         public const string UrlSeparator = "@@";
         public const string UrlTrimItemSeparator = "$$";
         private static string _mongoConnectionString = "mongodb://192.168.86.131/?socketTimeoutMS=2400000";
 
         public MongoServer Server { get; private set; }
 
         public MongoDB.Driver.MongoDatabase Database { get; private set; }
 
         public MongoCollection<BsonDocument> DataSet { get; set; }
 
         public MongdbHelper(string dbName, string tableName)
         {
             Server = MongoServer.Create(_mongoConnectionString);
             Database = Server.GetDatabase(dbName);
             DataSet = Database.GetCollection(tableName);
         }
 
         public MongdbHelper(string connectionString, string dbName, string tableName)
         {
             Server = MongoServer.Create(connectionString);
             Database = Server.GetDatabase(dbName);
             DataSet = Database.GetCollection(tableName);
         }
 
         public void Dispose()
         {
             Server.Disconnect();
         }
 
         /// <summary>
         /// 
         /// </summary>
         /// <param name="startTime"></param>
         /// <param name="lastTime"></param>
         /// <returns></returns>
         public static List<TopQuestion> GetTopQuestions(string startTime, string lastTime)
         {
 
             List<TopQuestion> noAnserQuestions = null;
 
             DateTime coStartTime = new DateTime();
             DateTime coLastTime = new DateTime();
 
             if (startTime != "NULL")
             {
                 coStartTime = DateTime.ParseExact(startTime, "yyyy-MM-dd", CultureInfo.InvariantCulture).AddHours(-8);
                 coLastTime = DateTime.ParseExact(lastTime, "yyyy-MM-dd", CultureInfo.InvariantCulture).AddHours(-8);
             }
 
             try
             {
                 using (MongdbHelper db = new MongdbHelper("CQAOnlineLog", "NoAnswerQuestion"))
                 {
 
                     if (startTime != "NULL")
                     {                                                   //Convert.ToDateTime(q.LastEditTime).CompareTo(coStartTime) >= 0
                         noAnserQuestions = db.DataSet.AsQueryable<TopQuestion>().Where(q => q.LastEditTime >= coStartTime.AddHours(-8)
                                                                                     && q.LastEditTime < coLastTime.AddDays(1)
                                                                                     && q.IsExport != 1).ToList();
                     }
                     else 
                     {
                         noAnserQuestions = db.DataSet.AsQueryable<TopQuestion>().Where(q => q.IsExport != 1).ToList();
                     }
                 }
  
                 var queryIds = noAnserQuestions.Select(q => q.Id).ToList();
 
                 using (MongdbHelper helper = new MongdbHelper("CQAOnlineLog", "NoAnswerQuestion"))
                 {
                     queryIds.ForEach(q =>
                     {
                         helper.DataSet.FindAndModify(new QueryDocument("_id",q),
                         SortBy.Null,
                         new UpdateBuilder().Set("IsExport", 1),
                         false, true);
                     });
                 }
 
                 return noAnserQuestions;
             }
             catch
             {
                 return null;
             }
         }
 
         public static List<T> GetOnColumn<T>(string dbName, string tableName, string column, Func<object, T> convert)
         {
             try
             {
                 using (MongdbHelper db = new MongdbHelper(dbName, tableName))
                 {
                     List<T> results = new List<T>();
                     var r = db.DataSet.FindAll();
                     r.SetFields(column);
                     foreach (var c in r)
                     {
                         results.Add(convert(c[column]));
                     }
 
                     return results;
                 }
             }
             catch
             {
                 return null;
             }
         }
 
         public static bool IsExist(string dbName, string tableName, string column, object value)
         {
             try
             {
                 using (MongdbHelper db = new MongdbHelper(dbName, tableName))
                 {
                     IMongoQuery query = new QueryDocument() { { column, value.ToString() } };
                     var results = db.DataSet.FindOne(query);
                     return results != null;
                 }
             }
             catch
             {
                 return false;
             }
         }
     }
 }

知识共享许可协议
《在C#中访问MongoDB,查询与修改》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     176.74ms