Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB stores generic List collections

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Protected virtual MongoConfigurationBuilder GetConfiguration ()

{

Var builder = new MongoConfigurationBuilder ()

Builder.ReadConnectionStringFromAppSettings ("tests")

Return builder

}

/ / data mapping class (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 ()

});

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 ()

/ / you can open comments if you need to add information.

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 + = "number:" + listd [1]

List items = (listd [2] as List)

Foreach (var item1 in items)

{

Var item2 = item1.ToList () [1] .value

Showinfo + = "name:" + item2

}

}

Console.WriteLine (showinfo)

}

Catch {}

}

Console.ReadKey ()

}

# region Collection Information

# region add information to the combination

Private static ListInfo BindVendor ()

{

Vendor square = new vendor ()

{

Vendorid = 2

Vendorname = "West suburb Auto parts City"

Itemid = DateTime.Now.Second

}

List list = new List ()

List.Add (square)

ListInfo listinfo = new ListInfo ()

{

Key = 7888

Value = list

}

Return listinfo

}

# endregion

# region entity Class

Class vendor

{

Public int vendorid {get; set;}

Public string vendorname {get; set;}

Public int itemid {get; set;}

}

# endregion

# endregion

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report