1> mongodb下载地址:
http://www.mongodb.org/downloads
官方下载不了,可以到百度共享盘里面下载
MongoDB 2.6.5
Windows 64位: http://pan.baidu.com/s/1qW0nCpI
Windows 32位:http://pan.baidu.com/s/1qWmG2NM
Linux 64位: http://pan.baidu.com/s/1CRxzo
转自:http://mongoing.com/mongodb-downloads
2> mongodb c#驱动 下载地址(下载完成后,需自己编译项目,然后用项目编译后生成的DLL文件即可。)
https://github.com/samus/mongodb-csharp
其实是有二种驱动:可以参考 http://blog.csdn.net/dannywj1371/article/details/7440916
3>我用的mongodb.dll 文件
使用过程中可能遇到问题
reason: errno:10061 No connection could be made because the target machine actively refused it.
解决方式:
sc.exe create MongoDB binPath=”d:\MongoDB\bin\mongod.exe –service –config=d:\MongoDB\mongod.cfg” DisplayName=”MongoDB 2.6 Standard” start=”auto”
http://www.cnblogs.com/yuluhuang/p/3982989.html
public class MongoDBHelper<T> where T : class { string connectionString = string.Empty; string databaseName = string.Empty; string collectionName = string.Empty; static MongoDBHelper<T> mongodb; public MongoDBHelper(string connectionString, string databaseName, string collectionName) { this.collectionName = collectionName; this.connectionString = connectionString; this.databaseName = databaseName; } public MongoConfiguration configuration { get { var config = new MongoConfigurationBuilder(); config.Mapping(mapping => { mapping.DefaultProfile(profile => { profile.SubClassesAre(t => t.IsSubclassOf(typeof(T))); }); mapping.Map<T>();//将类型添加到集合中 mapping.Map<T>(); }); config.ConnectionString(connectionString); return config.BuildConfiguration(); } } public void Insert(T t) { using (Mongo mongo = new Mongo()) { try { mongo.Connect(); var db = mongo.GetDatabase(databaseName); var collection = db.GetCollection<T>(collectionName); collection.Insert(t, true); mongo.Disconnect(); } catch (Exception) { mongo.Disconnect(); throw; } } } public void Update(T t, Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo()) { try { mongo.Connect(); var db = mongo.GetDatabase(databaseName); var collection = db.GetCollection<T>(collectionName); collection.Update(t, func, true); mongo.Disconnect(); } catch (Exception) { mongo.Disconnect(); throw; } } } public List<T> GetList(int pageIndex, int pageSize, int needPageCount, Expression<Func<T, bool>> func, out int pageCount) { pageCount = 0; using (Mongo mongo = new Mongo()) { try { mongo.Connect(); var db = mongo.GetDatabase(databaseName); var collection = db.GetCollection<T>(collectionName); pageCount = Convert.ToInt32(collection.Count() / pageSize); var modelList = collection.Linq().Where(func).Skip(pageSize * (pageIndex - 1)).Take(needPageCount * pageSize).Select(i => i).ToList(); mongo.Disconnect(); return modelList; } catch (Exception) { mongo.Disconnect(); throw; } } } public List<T> ListAll(Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo()) { try { mongo.Connect(); var db = mongo.GetDatabase(databaseName); var collection = db.GetCollection<T>(collectionName); var list = collection.Linq().Select(i => i).ToList(); mongo.Disconnect(); return list; } catch (Exception) { mongo.Disconnect(); throw; } } } public T Single(Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo()) { try { mongo.Connect(); var db = mongo.GetDatabase(databaseName); var collection = db.GetCollection<T>(collectionName); var single = collection.Linq().FirstOrDefault(func); mongo.Disconnect(); return single; } catch (Exception) { mongo.Disconnect(); throw; } } } public void Delete(Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo()) { try { mongo.Connect(); var db = mongo.GetDatabase(databaseName); var collection = db.GetCollection<T>(databaseName); //这个地方一定要加上T参数,不然会当成object类型的来处理,导致删除失败。 collection.Remove<T>(func); mongo.Disconnect(); } catch (Exception) { mongo.Disconnect(); throw; } } } }