zoukankan      html  css  js  c++  java
  • [总结].net操作MongoDb通用基础类1:

        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.以上是底层代码

  • 相关阅读:
    【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
    【2019.8.8 慈溪模拟赛 T2】query(query)(分治+分类讨论)
    【CometOJ】Comet OJ
    【CodeForces】CodeForcesRound576 Div1 解题报告
    【2019.8.12 慈溪模拟赛 T2】汪哥图(wang)(前缀和)
    【2019.8.12 慈溪模拟赛 T1】钥匙(key)(暴力DP)
    【2019.8.9 慈溪模拟赛 T2】摘Galo(b)(树上背包)
    【BZOJ3171】[TJOI2013] 循环格(网络流)
    【AtCoder】AtCoder Grand Contest 035 解题报告
    【2019.8.11上午 慈溪模拟赛 T2】十七公斤重的文明(seventeen)(奇偶性讨论+动态规划)
  • 原文地址:https://www.cnblogs.com/Zive/p/4276539.html
Copyright © 2011-2022 走看看