MongoDB Learning Notes (iv) describing data relationships with MongoDB document structure

Source: Internet
Author: User
MongoDB Collections (collection) can be viewed as tables of relational databases, and document objects (documents) can be viewed as a record of relational databases. But the two are not exactly equal. The structure of the table is fixed, the MongoDB set does not have this constraint, and the document object that is stored in the collection can even embed the subdocument, or "child collections". They can eventually be described in a format similar to Bjson. We are here today to analyze the unique data management approach that MongoDB this feature brings. We still take Samus drive as an example to analyze, Samus Drive support two ways to access the database, the basic Way and LINQ way, the basic way in the previous article to introduce, LINQ way I do not want to explain the application instance alone, this article I will use two ways to compare the introduction. One, the collection operations that contain subdocuments

There is such an application scenario, a website provides member login function, the user needs to register the account to be able to enjoy the member service, but the registrant may because the user data form entry item is too big to fill in, therefore the user information divides into the main information and the detailed information two items, the initial registration only needs to fill in the main material to be We intend to design the detailed information as a subdocument store.

  1) LINQ mode Implementation

1. Create a new data description class that describes user information view source print?

01 <summary>
02 User main information
03 </summary>
04 public class UserInfo
05 {
06 public string UserId {get; set;}
07 public string UserName {get; set;}
08 public string PassWord {get; set;}
09 Public Detail Detail {get; set;}
10 }
11
12 <summary>
13 User Details
14 </summary>
15 public class Detail
16 {
17 public string Address {get; set;}
18 public int Age {get; set;}
19 public string Email {get; set;}
20 }

2. We want to create a new user business Operation class "USERBLL". This time to let the driver know the UserInfo class describes the field information for user data, and the Getmongo () method implements the configuration steps, Userbll complete code as follows: View source print?

01 public class USERBLL
02 {
03 public string connectionString = "Mongodb://localhost";
04 public string databaseName = "MyDatabase";
05
06 Private Mongo Mongo;
07 Private Mongodatabase mongodatabase;
08
09 Note that the generic type is "UserInfo"
10 Private mongocollection<userinfo> mongocollection;
11
12 Public Userbll ()
13 {
14 MONGO = Getmongo ();
15 Mongodatabase = MONGO. Getdatabase (DatabaseName) as mongodatabase;
16 Mongocollection = mongodatabase.getcollection<userinfo> () as mongocollection<userinfo>;
17 Mongo. Connect ();
18 }
19 ~USERBLL ()
20 {
21st Mongo. Disconnect ();
22 }
23
24 <summary>
25 Configure MONGO to map the class UserInfo to the collection
26 </summary>
27 Private Mongo Getmongo ()
28 {
29 var config = new Mongoconfigurationbuilder ();
30 Config. Mapping (Mapping =>
31 {
32 Mapping. DefaultProfile (Profile =>
33 {
34 Profile. Subclassesare (t => t.issubclassof (typeof (UserInfo)));
35 });
36 Mapping. Map<userinfo> ();
37 });
38 Config. ConnectionString (ConnectionString);
39 Return to new Mongo (config. Buildconfiguration ());
40 }
41 }

3. Then, define a Method "Insertsomedata ()" In the "Userbll" class to insert some data: Show Source view source print?

01 <summary>
02 Insert some data
03 </summary>
04 public void Insertsomedata ()
05 {
06 UserInfo userInfo1 = new UserInfo ()
07 {
08 UserId = "1001",
09 UserName = "John",
10 PassWord = "123456"
11 };
12 Mongocollection.save (USERINFO1);
13
14 UserInfo UserInfo2 = new UserInfo ()
15 {
16 UserId = "1002",
17 UserName = "Dick",
18 PassWord = "123456",
19 Detail = new Detail ()
20 {
21st Address = "Hubei",
22 &n
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.