• C#操作MongoDB


    1.Web.config配置:
    <configuration>
      <connectionStrings>
        <add name="MongoSessionServices" connectionString="mongodb://192.168.2.88:27017" />
      </connectionStrings>
    </configuration>
    2.建立BaseDal.cs
    public class BaseDal
    {
        protected static IMongoDatabase MogoDatabase
        {
            get
            {
                return new MongoClient(ConfigurationManager.ConnectionStrings["MongoSessionServices"].ToString()).GetDatabase("数据库名称");
            }
        }
    
        /// <summary>
        /// MongoDb查询
        /// </summary>
        /// <typeparam name="T">对象名称</typeparam>
        /// <param name="collectionName">表名</param>
        /// <returns></returns>
        protected static IQueryable<T> IQueryableInit<T>(string collectionName) where T : new()
        {
            var colls = MogoDatabase.GetCollection<T>(collectionName);
            var query = from n in colls.AsQueryable() select n;
            return query;
        }
    }
    3.建立Model
    public class Text
    {
        //mongodb生成的_id可由下列方式生成
        //[BsonId]
        //public ObjectId Id { get; set; } //时间戳,机器ID,进程ID和序列号组成的12字节值
        [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
        public string Id { get; set; }  //由字符串组成
        //[BsonId(IdGenerator = typeof(CounterIdGenerator))]
        //public int Id { get; set; } //由数字组成
        [BsonElement("name")]  //mongodb中的名称
        public string Name { get; set; }
    }
    
    public class CounterIdGenerator : IIdGenerator
    {
        private static int _counter = 0;
        public object GenerateId(object container, object document)
        {
            return _counter++;
        }
    
        public bool IsEmpty(object id)
        {
            return id.Equals(default(int));
        }
    }
    4.查询
    public class GenericArticle_Dal : BaseDal
    {
    
        public static IQueryable<MongoDB_Models.GenericArticle> GetInit()
        {
            var query = IQueryableInit<MongoDB_Models.GenericArticle>(MongoDB_Models.Collections.GenericArticle);
            return query;
        }
    }
    static void Main(string[] args)
    {
        var list=GenericArticle_Dal.IQueryable<MongoDB_Models.GenericArticle>().ToList();
    }
    5.多表查询
    public class Video_Dal
    {
        public static IQueryable<VideoExt> GetInit(IMongoCollection<Video> coll1, IMongoCollection<VideoList> coll2)
        {
            var temp = from n in coll1.AsQueryable()
                       join f in coll2.AsQueryable() on n.ID equals f.ID
                       select new HeavenVideoExt()
                       {
                           Id = n.Id,
                           ModelId = n.ModelId,
                           AddMongoDateTime = n.AddMongoDateTime,
                           ID = n.ID,
                           Title = n.Title,
                           UploadImgPath = n.UploadImgPath,
                           VideoList = f
                       };
            return temp;
        }
    }
    var colls1 = MogoDatabase.GetCollection<Video>("Video");
    var colls2 = MogoDatabase.GetCollection<VideoList>("VideoList");
    var list = Video_Dal.GetInit(colls1, colls2).ToList();
  • 相关阅读:
    FFmpeg在JAVA中的使用以及Process.waitFor()引发的阻塞问题
    分析自己遇到的Excel导出报NullpointException问题
    C# 获取文件路径
    List的方法和属性 方法或属性 作用
    asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    .NET 垃圾回收与内存泄漏
    .NET多线程总结和实例介绍
    C#中释放数据库连接资源
    PHP
    sqlconnection dispose()与close()的区别
  • 原文地址:https://www.cnblogs.com/genesis/p/7874220.html
Copyright © 2020-2023  润新知