The difference between cloud development databases and traditional databases

In the applets · cloud development, the core is the three components: database, cloud storage and cloud functions, starting today, we will start every other day more column, Cloud development 101, in the first week, we will start with the most core database.

Introduction to cloud development database

First of all, let’s look at the cloud database development, cloud development database is developed by cloud development team to provide cloud user database services, developers will be able to in a small procedures, functions of cloud environments, such as through comprehensible to function calls, to get to the corresponding data, convenient developers to quickly complete a part of the business logic about the database.

During development, you can use methods such as wx.cloud.database().collection(‘data’).where({“age”:10}).get() to retrieve information from the database without having to complete database requests through the API provided by the server. The power of data query is devolved to the small program side to speed up the iteration efficiency of application.

Introduction to the underlying technology of cloud development database

The MongoDB database in NoSQL (Not Only SQL) database scheme is used for cloud development database. MongoDB database is the best NoSQL database developed in the industry at present, which can allow developers to complete the construction of database structure in SQL and NoSQL two ways, and quickly complete the development of applications.

No (with SQL

In the traditional Web application development process, we mostly use SQL database, such as Oracle, SQLite, MySQL, MSSQL, etc., but the MongoDB used in cloud development is a completely different database scheme, so the database structure design is also different. The wrong model of thinking can cause you endless trouble in the subsequent application development process. Therefore, this article introduces you to the magic of the NoSQL world.

Features brought by Schemaless

When we use SQL database development, we need to first design the structure of the database, the structure of the data table, etc., and NoSQL type database, therefore, let us in the development of time, there will be a different development mode. We do not need to add table structure in the application development, we only need to add and delete new fields at any time according to our own use situation, to fulfill our own business needs, it is this freedom, cloud development has the characteristics of rapid development, rapid iteration.

Cloud development database structure design ideas

Because the database type used by cloud development is different from the database type we are familiar with, therefore, at the time of development, we also need to modify our database structure accordingly to adapt to the characteristics of NoSQL database, so as to reduce the complexity of programming, good and fast to meet their business needs.

Different from SQL database, MongoDB database considers distributed and multi-node storage as its storage structure from the beginning of design, and its Best Practice is “space for time”. Therefore, when designing application data structure, it should not consider the data storage space of the application, but consider more. How to query data faster.

However, database design can not completely pursue time, but also consider the complexity of programming, balance time, space and programming complexity, in a better way to complete their own database design.

For example, due to the extreme pursuit of space change time of data, it is obviously unreasonable to design only one set of various data of the whole data system, and all the data are hung in one set. Such storage will lead to trouble in subsequent iterations of application.

Similarly, the extreme lack of space for time is also a bad choice. If you put all your data into separate collections, you will not make good use of NoSQL database features and will make your subsequent programming difficult.

Therefore, when using cloud database development, we need to think about our business development direction, separate the scenarios that may be used, and think about the database structure of the application, so as to ensure that there are no problems in the subsequent development of our application.

Frequently asked Questions about cloud development database usage

In the actual application, we also see that many people are not familiar with NoSQL database design concept, in the actual development process, there are many problems, here we discuss one by one.

Self-built primary key properties

In the MongoDB database, ObjectID is used for data storage. Therefore, the ID of data in the ObjectID is not 1, 2, 3, 4, but a hash value similar to A718A0F318D76. Each time the ID is added, it is queried again. After obtaining the latest value, it is added again.

In fact, there is a more elegant and convenient tool to do this, which is to add a new field, CREated_AT, whose value is set to Timestamp of the current time. When you need to sort the data by the newly added data, you can use this field to sort the data in reverse order. At the same time, because the data uses the current time data, you can also use this field for the “created in XX year, XX month, XX date” function to fulfill your business requirements. In addition to sorting the data directly, it can also be used to export the data by date, such as filtering out the data in a specific period.

Created_at is much easier to use than an incremented ID, and reduces the number of queries because you don’t need to retrieve the value of this field from the previous record.

Time storage problem

In our business logic development, the acquisition of time data is indispensable. Many people are accustomed to using a visual date data, so they will set the date field in the database as 2019-09-09, so that it can be directly output to the database when used. However, in fact, in the development process, It is recommended that you store Timestamp as a specific time.

This is because the data of 2019-09-09 is not a field that can be used for sorting. In the subsequent development, because you use string as the time, if you need to sort the data, sorting will not be possible or there will be sorting errors. In addition, because you store time as a string, subsequent changes in the presentation form of your business requirements can result in you spending a lot of time modifying all data time, or making multiple format transformations as data is read and stored, which can be a hassle.

Demand for storage time have, therefore, we always recommend to use timestamp to storage, because the timestamp is a digital type of data, so you can directly to the size of the comparison, at the same time, because the timestamp data is uniform around the world, if you have the demand of the globalization, the application of the follow-up can be very good support.

There is no way to tell whether to split into separate data sets for storage

When developing data applications for cloud development, we found that many developers questioned whether it was necessary to separate the data I used into a Collection for data storage.

This problem can be judged by some simple questions:

  1. Is there a need to sort the data you are using?
  2. Do you need to modify the data you are using?
  3. Is the data you use used elsewhere than here?

Let’s take a few examples here. For example, we have a requirement to add comments to articles in a content publishing system. How do we accomplish this part?

If your comment data is not sorted, modified, and only used here, you can consider putting the comment data in a sub-attribute of the article data, which can effectively complete the data query, when you query the article, directly query the comment data out, and display, very convenient.

If you review the data with the demand of the sort, modify, and other places to use, then suggest you will comment alone in a collection of data, in order to finish sorting, updating, and in subsequent calls, if you put it in the article, the data will have a lot of problems in the subsequent queries, extremely inconvenient to operate, ACTS up trouble for yourself.

Of course, case by case analysis, if you in the analysis completed, still do not have the answer, you can put forward your question in the public account background, we will reply to you as soon as possible.

conclusion

In this article, we share the difference between the NoSQL database used by cloud development and the traditional SQL database, and the difference in the development experience because of this difference, understanding these basic content, will help you better use the cloud development database to develop your own products.

If you have any questions about cloud development, please leave your questions in the comments section and we will answer them.


If you want to know more about the cloud development of CloudBase related technical stories/technical combat experience, please scan the code to follow [Tencent cloud development] public account ~

About Cloud Development

CloudBase is a product solution of cloud integration. It adopts serverless architecture, eliminates operation and maintenance transactions such as environment construction, supports one cloud and multiple terminals, and helps to quickly build small programs, Web applications and mobile applications.

  • Technical documentation: www.cloudbase.net/
  • Wechat search: Tencent Cloud Development, get the latest progress of the project