[总结].net操作MongoDb通用基础类1:_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > [总结].net操作MongoDb通用基础类1:

[总结].net操作MongoDb通用基础类1:

 2015/2/6 11:54:58  xiaoxiao001  程序员俱乐部  我要评论(0)
  • 摘要:publicclassMongoDBHelper{//定义Mongo服务privateMongoServermongo=null;//获取databaseName对应的数据库,不存在则自动创建privateMongoDatabasemongoDatabase;///<summary>///Mongo数据库连接///</summary>publicMongoDBHelper(){mongo=MongoServer.Create(MongoDBConfig
  • 标签:总结 .net net 操作 MongoDB
class="brush:csharp;gutter:true;">    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.以上是底层代码

 

发表评论
用户名: 匿名