A step-by-step introduction to the development framework based on SqlSugar (27)–MongoDB-based database operation integration
The development framework of SqlSugar itself is mainly based on the conventional relational database design framework. It supports the access of multiple database types, such as SqlServer, MySQL, Oracle, PostgreSQL, SQLite and other databases. The MongoDB database of non-relational databases can also be integrated as an extension. In the development framework, the relevant 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 SqlSugar development framework.
SqlSugar’s development framework itself is mainly based on a conventional relational database design framework, supporting access to multiple database types, such as SqlServer, MySQL, Oracle, PostgreSQL, SQLite and other databases. MongoDB databases that are non-relational databases can also be used. Integrated into the development framework as an extension, it well encapsulates related basic operation functions through the inheritance relationship of the base class, 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 SqlSugar development framework.
1. A brief introduction to MongDB
MongoDB is a high-performance, open source, schema-less commonly used non-relational database product written in C++. It is the most feature-rich and most relational database among non-relational databases. It extends many functions of relational databases, such as auxiliary indexes, range queries, sorting, etc.
MongoDB is a product between a relational database and a non-relational database. It is the most feature-rich among non-relational databases and is most similar to a relational database. The data structure it supports is very loose and 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 an object-oriented query language. It can almost implement most functions similar to single-table queries in relational databases, and it also supports indexing of data. And MongoDB-4.2 has supported distributed transaction functions since version 4.2.
The MongoDB database has several simple concepts that need to be understood.
1) database
in MongoDB has the same concept as the “database” we are familiar with (for Oracle, it is schema). 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. Collections are basically the same as tables in the traditional sense, and they can simply be regarded as the same thing.
3) A collection is composed of zero or more documents
(documents). Likewise, a document can be viewed as a row
.
4) The 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, both to improve query efficiency.
6) Cursors
(cursor) is different from the five concepts above, but it is very important and often overlooked. The most important thing you need to understand is that cursors are When you ask MongoDB to get data, it will return you a pointer to the result set instead of the real data. We call this pointer a cursor. We can use the cursor to do whatever we want, such as counting or crossing rows. without dragging down the real data and operating on the real data.
Their comparison chart is shown below.
Data is stored in Json format in Mongodb, as shown below is one of the record contents.
BSON format
Bson is a Json-like binary storage format, 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 disadvantage is The space utilization 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, which is generally a cstring type, and the byte representation is cstring::= (byte*) “/x00”, Among them, * represents zero or more bytes, /x00 represents the terminator; the following “world” is the value, and its type is generally string, double, array, binary data and other types.
The MongoDB database itself supports drivers for multiple development languages. MongoDB has official drivers as follows:
id)
{
//Check user Whether there is permission, otherwise MyDenyAccessException will be thrown
base.CheckAuthorized(AuthorizeKey.ViewKey) ;
return await span> _service.GetAsync(id);
}
………….
Finally, we can start Swagger to test the corresponding interface. In fact, it can also be integrated into the UI for testing processing. After we install the MongoDB database management tool, we can query the data of the corresponding database in MongoDBCompass.
////// Customer information controller Object (based on MongoDB), based on BaseApiController, requires custom interface processing /// [ApiController] [Route(" api/MongoCustomer" )] public class span> MongoCustomerController : BaseApiController { privateICustomerService _service; /// /// constructor and inject Basic interface object /// /// public MongoCustomerController(ICustomerService service) { this._service = service; } /// /// Get all records /// [HttpGet] [Route(" all")] public virtual span> async Task<ListResultDto>GetAllAsync() { //Check user Whether there is permission, otherwise MyDenyAccessException will be thrown base.CheckAuthorized(AuthorizeKey.ListKey) ; return await span> _service.GetAllAsync(); }
And if it inherits from MongoBaseController, it will have all the controller methods exposed by the base class MongoBaseController.
////// Customer information controller Object (based on MongoDB), based on MongoBaseController, with regular CRUD operation interface /// [ApiController] [Route(" api/MongoCustomer2" )] public class span> MongoCustomer2Controller : MongoBaseController { /// /// constructor and inject Basic interface object /// /// public MongoCustomer2Controller(ICustomerService service) : base (service) { } }
I also introduced the related use of this database a few years ago. The essay is as follows. You can also learn more if necessary.
MongoDB database development application based on C# (4)–Installation and use of Redis Wu Huacong 2016-01-12
MongoDB database development application based on C# (3)–Asynchronous interface for C# development of MongoDB database Wu Huacong 2016-01-12
MongoDB database development application based on C# (2)–C# development of MongoDB database Wu Huacong 2016-01-06
MongoDB database development and application based on C# (1)-Basic knowledge and use of MongoDB database Wu Huacong 2016-01-05
Focus on code generation tools, .Net/.NetCore framework architecture and software development, as well as various Vue.js front-end technology applications. He is the author of Winform development framework/hybrid development framework, WeChat development framework, Bootstrap development framework, ABP development framework, SqlSugar development framework and other framework products.
Please indicate the source for reprinting: Writer: Wu Huacong http://www.iqidi.com
n>
[ApiController]
[Route(“ api/MongoCustomer2“ )]
public class span> MongoCustomer2Controller : MongoBaseController
{
///
/// constructor and inject Basic interface object
///
///
public MongoCustomer2Controller(ICustomerService service) : base (service)
{
}
}
I also introduced the related use of this database a few years ago. The essay is as follows. You can also learn more if necessary.
MongoDB database development application based on C# (4)–Installation and use of Redis Wu Huacong 2016-01-12
MongoDB database development application based on C# (3)–Asynchronous interface for C# development of MongoDB database Wu Huacong 2016-01-12
MongoDB database development application based on C# (2)–C# development of MongoDB database Wu Huacong 2016-01-06
MongoDB database development and application based on C# (1)-Basic knowledge and use of MongoDB database Wu Huacong 2016-01-05
Focus on code generation tools, .Net/.NetCore framework architecture and software development, as well as various Vue.js front-end technology applications. He is the author of Winform development framework/hybrid development framework, WeChat development framework, Bootstrap development framework, ABP development framework, SqlSugar development framework and other framework products.
Please indicate the source for reprinting: Writer: Wu Huacong http://www.iqidi.com