public class MongoDBHelper { //定義Mongo服務 private MongoServer mongo = null; //獲取databaseName對應的數據庫,不存在則自動創建 private MongoDatabase mongoDatabase; /// <summary> /// Mongo 數據庫連接 /// </summary> public MongoDBHelper() { mongo = MongoServer.Create(MongoDBConfig.gConnectionString); mongoDatabase = mongo.GetDatabase(MongoDBConfig.gDatabaseName) as MongoDatabase; mongo.Connect(); } /// <summary> /// Mongo 數據庫斷開連接 /// </summary> public void CloseConnection() { if (this.mongo != null) { this.mongo.Disconnect(); this.mongo = null; } } /// <summary> /// 根據條件查找所有記錄 /// </summary> /// <returns></returns> public IEnumerable<BsonDocument> FindAll(IMongoQuery pQuery,int currentpage,int pagesize, string pTable) { //獲取collectionName對應的集合,不存在則自動創建 MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; return mongoCollection.Find(pQuery).SetLimit(pagesize).SetSkip(pagesize * currentpage).ToList(); ; } /// <summary> /// 查找所有記錄 /// </summary> /// <returns></returns> public IEnumerable<BsonDocument> FindAll(int currentpage,int pagesize,string pTable) { //獲取collectionName對應的集合,不存在則自動創建 MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; return mongoCollection.FindAll().SetLimit(pagesize).SetSkip(pagesize*currentpage).ToList(); } /// <summary> /// 根據條件查找所有記錄 /// </summary> /// <returns></returns> public IEnumerable<BsonDocument> FindAll(IMongoQuery pQuery, string pTable) { //獲取collectionName對應的集合,不存在則自動創建 MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; return mongoCollection.Find(pQuery); } /// <summary> /// 查找所有記錄 /// </summary> /// <returns></returns> public IEnumerable<BsonDocument> FindAll(string pTable) { //獲取collectionName對應的集合,不存在則自動創建 MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; return mongoCollection.FindAll(); } /// <summary> /// 增加一條記錄 /// </summary> /// <param name="doc"></param> public void Add(object obj, string pTable) { //獲取collectionName對應的集合,不存在則自動創建 MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; mongoCollection.Insert(obj); } /// <summary> /// 刪除一條記錄 /// </summary> public void Delete(string id, string pTable) { //獲取collectionName對應的集合,不存在則自動創建 MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; mongoCollection.Remove(new QueryDocument { { "_id", id } }); } #region 獲取當前連接數據庫的指定集合【依據類型】 /// <summary> /// 獲取當前連接數據庫的指定集合【依據類型】 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public MongoCollection<T> GetCollection<T>(string name,WriteConcern writeConcern) where T : class { return this.mongoDatabase.GetCollection<T>(name,writeConcern); } /// <summary> /// 獲取當前連接數據庫的指定集合【根據指定名稱】 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name">集合名稱</param> /// <returns></returns> public MongoCollection<T> GetCollection<T>(string pTableName) where T : class { return this.mongoDatabase.GetCollection<T>(pTableName); } #endregion #region GridFs 文件處理 /// <summary> /// 保存2進制數據到db裡面 /// </summary> /// <param name="byteFile"></param> /// <returns></returns> public string GridFsSave(byte[] byteFile) { string filename = Guid.NewGuid().ToString(); //這裡GridFile構造函數有個重載,bucket參數就是用來替換那個創建集合名中默認的"fs"的。 MongoGridFS gridFile = new MongoGridFS(mongoDatabase); using (MongoGridFSStream gridFileStream = gridFile.Create(filename)) { gridFileStream.Write(byteFile, 0, byteFile.Length); } return filename; } public void SaveGridFsFile(BsonDocument doc, string pTable) { MongoCollection<BsonDocument> mongoCollection =mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; mongoCollection.Save(doc); } /// <summary> /// 讀取為filename的文件 /// </summary> /// <param name="filename"></param> /// <returns></returns> public byte[] GridFsRead(string filename) { MongoGridFS gridFile = new MongoGridFS(mongoDatabase); MongoGridFSStream gridFileStream = gridFile.OpenRead(filename); byte[] bytes = new byte[gridFileStream.Length]; gridFileStream.Read(bytes, 0, bytes.Length); return bytes; } /// <summary> /// 根據條件取一條數據 /// </summary> /// <param name="fitter"></param> /// <param name="pTable"></param> /// <returns></returns> public BsonDocument GetFsFileInfo(QueryDocument fitter, string pTable) { MongoCollection<BsonDocument> mongoCollection = mongoDatabase.GetCollection<BsonDocument>(pTable) as MongoCollection<BsonDocument>; BsonDocument doc = mongoCollection.FindOne(fitter); return doc; } /// <summary> /// 刪除文件 /// </summary> /// <param name="filename"></param> public void GridFsDelete(string filename) { MongoGridFS gridFile = new MongoGridFS(mongoDatabase); gridFile.Delete(new QueryDocument("filename", filename)); } #endregion }
備注:由於最近使用.net開發一個項目,有機會用到了mongodb,我自己改成通用類
方法:
1.找到mongodb官網
2.下載mongodb.net的類庫,我用的是MongoDB.Bson.dll,MongoDB.Driver.dll的1.7版本,每個版本還不一樣.....mongodb對語言訪問支持正在加強.
3.以上是底層代碼