MongoDB存储泛型List<User>集合

发布时间:2020-06-03 09:45:07 作者:duoku
来源:网络 阅读:3303

 protected virtual MongoConfigurationBuilder GetConfiguration()

        {
            var builder = new MongoConfigurationBuilder();
            builder.ReadConnectionStringFromAppSettings("tests");
            return builder;
        }
        //数据映射类(key,value)
        class ListInfo
        {
            public int key { get; set; }
            public object value { get; set; }
        }
        static void Main(string[] args)
        {
            var config = new MongoConfigurationBuilder();
            // COMMENT OUT FROM HERE
            config.Mapping(mapping =>
            {
                mapping.DefaultProfile(profile =>
                {
                    profile.SubClassesAre(t => t.IsSubclassOf(typeof(ListInfo)));
                });
                mapping.Map<ListInfo>();
            });

            config.ConnectionString("Server=127.0.0.1");
            using (Mongo mongo = new Mongo(config.BuildConfiguration()))
            {
                mongo.Disconnect();
                mongo.Connect();
                try
                {
                    var db = mongo.GetDatabase("TestDb");
                    var collection = db.GetCollection<ListInfo>();
                    //添加信息 需要就可以打开注释
                    ListInfo list = BindVendor();
                    collection.Save(list);
                    var coll = db.GetCollection("ListInfo");
                    var info = coll.Find(new Document().Add("key", 7888)).Documents.ToList();
                    string showinfo = "";
                    foreach (Document item in info)
                    {

                        var listd = item.Values.ToList();
                        showinfo += "编号:" + listd[1];
                        List<Document> items = (listd[2] as List<Document>);
                        foreach (var item1 in items)
                        {
                            var item2 = item1.ToList()[1].Value;
                            showinfo += "名称:" + item2;
                        }

                    }
                    Console.WriteLine(showinfo);
                }
                catch { }
            }
            Console.ReadKey();
        }

        #region 集合信息

        #region 添加信息到结合
        private static ListInfo BindVendor()
        {
            vendor square = new vendor()
            {
                vendorid = 2,
                vendorname = "西郊汽配城",
                itemid = DateTime.Now.Second
            };
            List<vendor> list = new List<vendor>();
            list.Add(square);
            ListInfo listinfo = new ListInfo()
            {
                key = 7888,
                value = list
            };
            return listinfo;
        }
        #endregion
        #region 实体类
        class vendor
        {
            public int vendorid { get; set; }
            public string vendorname { get; set; }
            public int itemid { get; set; }
        }
        #endregion

        #endregion

推荐阅读:
  1. mongodb集合管理
  2. MongoDB固定集合

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongodb lis st

上一篇:R和Rstudio的下载

下一篇:Spring事务有几种实现方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》