A collection of MongoDB can be regarded as a table of a relational database, and a document object (document) can be regarded as a record of a relational database. But the two are not exactly equivalent. The structure of the table is fixed, and the MongoDB collection does not have this constraint; in addition, the document objects stored in the collection can even embed sub-documents, or “sub-collections”. They can all be described in a format similar to BJSON in the end. Let’s analyze the unique data management method brought by this feature of MongoDB today. Let’s take the samus driver as an example to analyze. The samus driver supports two ways to access the database, the basic way and the linq way. The basic way was introduced in the previous article. I don’t want to explain the application examples separately for the linq way. I will use two ways in this article. Ways to compare presentations.
1. Collection operations containing sub-documents
There is such an application scenario. A website provides the function of member login. Users need to register an account to enjoy membership services. However, registrants may give up filling in the user information form because the input items are too large. Therefore, user information is divided into main information and detailed information. Two, the initial registration only needs to fill in the main information on the line. We intend to design the details to be stored as subdocuments.
1) Linq implementation
1. Create a new data description class to describe user information
/// User profile
///
public class UserInfo
{
public string UserId {get;set; }
public string UserName {get;set; }
public string PassWord {get;set; }
public Detail Detail {get;set; }
}
///
/// User details
///
public class Detail
{
public string Address {get;set;}
public int Age {get;set; }
public string Email {get;set; }
}
2. We need to create a new user business operation class “UserBLL”. At this time, let the driver know that the UserInfo class describes the field information of “user profile”. The configuration steps are implemented in the GetMongo() method. The complete code of UserBLL is as follows:
public class UserBLL
{
public string cOnnectionString=”mongodb://localhost”;
public string databaseName = “myDatabase”;
private Mongo mongo;
private MongoDatabase mongoDatabase;
//Note that the generic type here is “UserInfo”
private MongoCollectionmongoCollection;
public UserBLL()
{
mOngo= GetMongo();
mOngoDatabase= mongo.GetDatabase(databaseName)as MongoDatabase;
mOngoCollection= mongoDatabase.GetCollection()as MongoCollection;
mongo.Connect();
}
~UserBLL()
{
mongo. Disconnect();
}
///
/// Configure Mongo, map the class UserInfo to the collection
///
private Mongo GetMongo()
{
var cOnfig=new MongoConfigurationBuilder();
config.Mapping(mapping =>
{
mapping.DefaultProfile(profile =>
{
profile.SubClassesAre(t => t.IsSubclassOf(typeof(UserInfo)));
});
mapping.Map();
});
config.ConnectionString(connectionString);
return new Mongo(config.BuildConfiguration());
}
}
3. Next, define a method “InsertSomeData()” in the “UserBLL” class to insert some data:
+ View Code
4. Define a data search method “Select”, which will search for all users whose address is in Hubei in the user’s detailed information:
the
/// Query the user information whose detailed data address is Hubei
///
public ListSelect()
{
return mongoCollection.Linq().Where(x => x.Detail.Address ==”Hubei”).ToList();
}
5. Also define a method to delete data, which will delete all data in the collection:
/// Delete all user information
///
public void DeleteAll()
{
mongoCollection. Remove(x => true);
}
6. Add the following code to the Main method:
the