A step-by-step introduction to the development framework based on SqlSugar (27) — integration of database operations based on MongoDB
The development framework of SqlSugar is mainly based on the framework of conventional relational database design, which supports the access of various database types, such as SqlServer, MySQL, Oracle, PostgreSQL, SQLite and other databases, and the non-relational database MongoDB database can also be used as an extended integration In the development framework, the related basic operation functions are well encapsulated through the inheritance relationship of the base class, which greatly reduces the code related to processing MongoDB and provides good development efficiency. This essay introduces how to integrate the development of MongoDB database in the development framework of SqlSugar.
The development framework of SqlSugar itself is mainly based on the framework of conventional relational database design, which supports the access of various database types, such as SqlServer, MySQL, Oracle, PostgreSQL, SQLite and other databases, as well as MongoDB databases of non-relational databases. It is integrated into the development framework as an extension, and the related basic operation functions are well encapsulated through the inheritance relationship of the base class, which greatly reduces the code related to processing MongoDB and provides good development efficiency. This essay introduces how to integrate the development of MongoDB database in the development framework of SqlSugar.
1. A brief introduction to MongDB
MongoDB is a high-performance, open source, schema-free common non-relational database product written in C++. It is the most feature-rich and relational database-like database among non-relational databases. It extends many functions of relational database, such as: auxiliary index, range query, sorting, etc.
MongoDB is a product between relational databases and non-relational databases. It is the most functional among non-relational databases and most similar to relational databases. The data structure it supports is very loose, which is a Bson format similar to Json, so it can store more complex data types.
The biggest feature of MongoDB is that the query language it supports is very powerful. Its syntax is somewhat similar to object-oriented query language. It can almost realize most of the functions similar to single-table query of relational database, and it also supports indexing of data. And the MongoDB-4.2 version has already supported the distributed transaction function.
MongoDB database has a few simple concepts that need to be understood.
1) database
in MongoDB has the same concept as the “database” we are familiar with (schema for Oracle). In a MongoDB instance, there can be zero or more databases, each serving as a higher-level container for storing data.
2) There can be zero or more collections
(collections) in the database. A collection is basically the same as a table in the traditional sense, and the two can be simply regarded as the same thing.
3) A collection is composed of zero or more documents
(documents). Similarly, a document can be viewed as a row
.
4) A document is composed of zero or more fields
(fields). , corresponding to the columns
of the relational database.
5) Indexes
(indexes) play the same role in MongoDB as they do in RDBMS, all to improve query efficiency.
6) Cursors
(cursor) is different from the above five concepts, but it is very important and often overlooked. The most important thing you need to understand is that the cursor is when When you ask MongoDB to get data, it will return you a pointer to the result set instead of real data. We call this pointer a cursor, and we can use the cursor to do whatever we want, such as counting or crossing rows. , without having to drag down the real data and operate on the real data.
A comparison diagram of them is shown below.
Data is stored in Json format in Mongodb, as shown below is one of the records.
BSON format
Bson is a binary storage format like Json, referred to as Binary Json. Like Json, it supports embedded document objects and array objects, but Bson has some data types that Json does not have, such as Date and BinData type.
Bson can be used as a storage form for network data exchange. This is somewhat similar to Google’s Protocol Buffer, but Bson is a schema-less storage form. Its advantage is high flexibility, but its disadvantages are The space utilization rate is not very ideal. Bson has three characteristics: lightweight, traversable, and efficient.
{“hello”:”world”} This is an example of Bson, where “hello” is the key name, it is generally cstring type, and the byte representation is cstring::= (byte*) “/x00” , Among them, * indicates zero or more bytes, /x00 indicates the terminator; the following “world” is the value value, and its type is generally string, double, array, binarydata and other types.
The MongDB database itself supports drivers of multiple development languages. MongoDB has official drivers as follows:
id)
{
//Check user Whether there is permission, otherwise throw MyDenyAccessException
base.CheckAuthorized(AuthorizeKey.ViewKey) ;
return await _service. GetAsync(id);
}
………….
Finally, we can start Swagger to test the corresponding interface. In fact, it can also be integrated in the UI for test processing. After we install the management tool of the MongoDB database, we can query the data of the corresponding database in MongoDBCompass.
////// Controller for customer information Object (based on MongoDB), based on BaseApiController, requires custom interface processing /// [ApiController] [Route(" api/MongoCustomer" )] public class MongoCustomerController : BaseApiController { private ICustomerService _service; /// /// constructor, and inject base interface object /// /// public MongoCustomerController(ICustomerService service) { this._service = service; } /// /// Get all records /// [HttpGet] [Route(" all")] public virtual</span span> async Task<ListResultDto> GetAllAsync() { //Check user Whether there is permission, otherwise throw MyDenyAccessException base.CheckAuthorized(AuthorizeKey.ListKey) ; return await _service. GetAllAsync(); }
And if it inherits from MongoBaseController, it will have all the controller methods exposed by the base class MongoBaseController.
////// Controller for customer information Object (based on MongoDB), based on MongoBaseController, with conventional CRUD operation interface /// [ApiController] [Route(" api/MongoCustomer2" )] public class MongoCustomer2Controller : MongoBaseController { /// /// constructor, and inject base interface object /// /// public MongoCustomer2Controller(ICustomerService service) : base (service) { } }
I also introduced the related use of this database a few years ago.
MongoDB database development and application based on C# (4) –Redis installation and use Wu Huacong 2016-01-12
MongoDB database development and application based on C# (3) — asynchronous interface of C# development of MongoDB database Wu Huacong 2016-01-12
C#-based MongoDB database development and application (2) — C# development of MongoDB database Wu Huacong 2016-01-06
MongoDB database development and application based on C# (1) — the basic knowledge and use of MongoDB database Wu Huacong 2016-01-05
the
Focus on code generation tools, .Net/.NetCore framework architecture and software development, and various Vue.js front-end technology applications. Author of Winform development framework/hybrid development framework, WeChat development framework, Bootstrap development framework, ABP development framework, SqlSugar development framework and other framework products.
Reprint please indicate the source: Author: Wu Huacong http://www.iqidi.com
pan>
[ApiController]
[Route(“ api/MongoCustomer2“ )]
public class MongoCustomer2Controller : MongoBaseController
{
///
/// constructor, and inject base interface object
///
///
public MongoCustomer2Controller(ICustomerService service) : base (service)
{
}
}
I also introduced the related use of this database a few years ago.
MongoDB database development and application based on C# (4) –Redis installation and use Wu Huacong 2016-01-12
MongoDB database development and application based on C# (3) — asynchronous interface of C# development of MongoDB database Wu Huacong 2016-01-12
C#-based MongoDB database development and application (2) — C# development of MongoDB database Wu Huacong 2016-01-06
MongoDB database development and application based on C# (1) — the basic knowledge and use of MongoDB database Wu Huacong 2016-01-05
the
Focus on code generation tools, .Net/.NetCore framework architecture and software development, and various Vue.js front-end technology applications. Author of Winform development framework/hybrid development framework, WeChat development framework, Bootstrap development framework, ABP development framework, SqlSugar development framework and other framework products.
Reprint please indicate the source: Author: Wu Huacong http://www.iqidi.com