1024programmer Mongodb MongoDB study notes (four) use MongoDB’s document structure to describe data relationships

MongoDB study notes (four) use MongoDB’s document structure to describe data relationships

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

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-study-notes-four-use-mongodbs-document-structure-to-describe-data-relationships/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索