返回顶部
首页 > 资讯 > 数据库 >MongoDB存储泛型List<User>集合
  • 507
分享到

MongoDB存储泛型List<User>集合

2024-04-02 19:04:59 507人浏览 安东尼
摘要

 protected virtual MonGoConfigurationBuilder GetConfiguration()      &nbs

 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

您可能感兴趣的文档:

--结束END--

本文标题: MongoDB存储泛型List<User>集合

本文链接: https://lsjlt.com/news/36130.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作