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?
06 |
public string UserId {get; set;} |
07 |
public string UserName {get; set;} |
08 |
public string PassWord {get; set;} |
09 |
Public Detail Detail {get; set;} |
17 |
public string Address {get; set;} |
18 |
public int Age {get; set;} |
19 |
public string Email {get; set;} |
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?
03 |
public string connectionString = "Mongodb://localhost"; |
04 |
public string databaseName = "MyDatabase"; |
07 |
Private Mongodatabase mongodatabase; |
09 |
Note that the generic type is "UserInfo" |
10 |
Private mongocollection<userinfo> mongocollection; |
15 |
Mongodatabase = MONGO. Getdatabase (DatabaseName) as mongodatabase; |
16 |
Mongocollection = mongodatabase.getcollection<userinfo> () as mongocollection<userinfo>; |
21st |
Mongo. Disconnect (); |
25 |
Configure MONGO to map the class UserInfo to the collection |
27 |
Private Mongo Getmongo () |
29 |
var config = new Mongoconfigurationbuilder (); |
30 |
Config. Mapping (Mapping => |
32 |
Mapping. DefaultProfile (Profile => |
34 |
Profile. Subclassesare (t => t.issubclassof (typeof (UserInfo))); |
36 |
Mapping. Map<userinfo> (); |
38 |
Config. ConnectionString (ConnectionString); |
39 |
Return to new Mongo (config. Buildconfiguration ()); |
3. Then, define a Method "Insertsomedata ()" In the "Userbll" class to insert some data: Show Source view source print?
04 |
public void Insertsomedata () |
06 |
UserInfo userInfo1 = new UserInfo () |
12 |
Mongocollection.save (USERINFO1); |
14 |
UserInfo UserInfo2 = new UserInfo () |
19 |
Detail = new Detail () |