hyytaojunming 发表于 2015-7-8 08:50:00

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

  在 C# 中使用 mongodb 来查询以及修改数据。



1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Globalization;
6 using MongoDB.Driver;
7 using MongoDB.Driver.Linq;
8 using MongoDB.Bson;
9 using MongoDB.Driver.Builders;
10 using Com.iFlytek.MarkerPlatform.DomainObjects;
11
12 namespace Com.iFlytek.MarkerPlatform.Helper
13 {
14   public class MongdbHelper : IDisposable
15   {
16         public const string UrlSeparator = "@@";
17         public const string UrlTrimItemSeparator = "$$";
18         private static string _mongoConnectionString = "mongodb://192.168.86.131/?socketTimeoutMS=2400000";
19
20         public MongoServer Server { get; private set; }
21
22         public MongoDB.Driver.MongoDatabase Database { get; private set; }
23
24         public MongoCollection DataSet { get; set; }
25
26         public MongdbHelper(string dbName, string tableName)
27         {
28             Server = MongoServer.Create(_mongoConnectionString);
29             Database = Server.GetDatabase(dbName);
30             DataSet = Database.GetCollection(tableName);
31         }
32
33         public MongdbHelper(string connectionString, string dbName, string tableName)
34         {
35             Server = MongoServer.Create(connectionString);
36             Database = Server.GetDatabase(dbName);
37             DataSet = Database.GetCollection(tableName);
38         }
39
40         public void Dispose()
41         {
42             Server.Disconnect();
43         }
44
45         ///
46         ///
47         ///
48         ///
49         ///
50         ///
51         public static List GetTopQuestions(string startTime, string lastTime)
52         {
53
54             List noAnserQuestions = null;
55
56             DateTime coStartTime = new DateTime();
57             DateTime coLastTime = new DateTime();
58
59             if (startTime != "NULL")
60             {
61               coStartTime = DateTime.ParseExact(startTime, "yyyy-MM-dd", CultureInfo.InvariantCulture).AddHours(-8);
62               coLastTime = DateTime.ParseExact(lastTime, "yyyy-MM-dd", CultureInfo.InvariantCulture).AddHours(-8);
63             }
64
65             try
66             {
67               using (MongdbHelper db = new MongdbHelper("CQAOnlineLog", "NoAnswerQuestion"))
68               {
69
70                     if (startTime != "NULL")
71                     {                                                   //Convert.ToDateTime(q.LastEditTime).CompareTo(coStartTime) >= 0
72                         noAnserQuestions = db.DataSet.AsQueryable().Where(q => q.LastEditTime >= coStartTime.AddHours(-8)
73                                                                                     && q.LastEditTime < coLastTime.AddDays(1)
74                                                                                     && q.IsExport != 1).ToList();
75                     }
76                     else
77                     {
78                         noAnserQuestions = db.DataSet.AsQueryable().Where(q => q.IsExport != 1).ToList();
79                     }
80               }
81
82               var queryIds = noAnserQuestions.Select(q => q.Id).ToList();
83
84               using (MongdbHelper helper = new MongdbHelper("CQAOnlineLog", "NoAnswerQuestion"))
85               {
86                     queryIds.ForEach(q =>
87                     {
88                         helper.DataSet.FindAndModify(new QueryDocument("_id",q),
89                         SortBy.Null,
90                         new UpdateBuilder().Set("IsExport", 1),
91                         false, true);
92                     });
93               }
94
95               return noAnserQuestions;
96             }
97             catch
98             {
99               return null;
100             }
101         }
102
103         public static List GetOnColumn(string dbName, string tableName, string column, Func convert)
104         {
105             try
106             {
107               using (MongdbHelper db = new MongdbHelper(dbName, tableName))
108               {
109                     List results = new List();
110                     var r = db.DataSet.FindAll();
111                     r.SetFields(column);
112                     foreach (var c in r)
113                     {
114                         results.Add(convert(c));
115                     }
116
117                     return results;
118               }
119             }
120             catch
121             {
122               return null;
123             }
124         }
125
126         public static bool IsExist(string dbName, string tableName, string column, object value)
127         {
128             try
129             {
130               using (MongdbHelper db = new MongdbHelper(dbName, tableName))
131               {
132                     IMongoQuery query = new QueryDocument() { { column, value.ToString() } };
133                     var results = db.DataSet.FindOne(query);
134                     return results != null;
135               }
136             }
137             catch
138             {
139               return false;
140             }
141         }
142   }
143 }
  
页: [1]
查看完整版本: 在C#中访问MongoDB,查询与修改