In the last article, we showed you how to install and simply use the Parse-Dashboard to manage our parse-Server service on a graphical page. In system development, we usually need to define our database table structure according to the service and form of the product. In MySQL, we usually initialize the database, as well as the table structure and fields in the form of the database initialization command in the file, while the table structure defined by MongoDB is usually not so simple. In this section we will take a hands-on look at defining our database structure and how to initialize the database using parse-Dashboard.
Design table structure
Determine field values
We usually analyze the requirements after receiving a new requirement, and then start designing the table structure. Now let’s assume that we need to design a user data table to store the basic information of the user:
The fields in the user table include:
The field names | The data type | If required | The default value |
---|---|---|---|
username | String | is | Null |
password | String | is | Null |
avatar | String | no | Null |
phone | String | is | Null |
String | is | Null | |
city | String | no | Null |
country | String | no | Null |
age | Number | no | 0 |
gender | Number | no | 0 |
After determining the field names and types in the data table, we can add field values to the parse-Dashboard based on the table structure.
The operation sample
- Into the
parse-dashboard
In theUser
In the table
-
Click Edit at the top of the list, and then click the Add a Column button in the middle of the page
-
Enter the field name in the dialog box that is displayed. For example, phone:
The type of the field to be added is String. Because String takes up less storage space in JavaScript, it is more appropriate to select String. Enter phone in the name of the field and select Yes in the required option
-
After the field is added, you can manually Add a row by clicking Add A Row on the page
Then fill in the default fields username and password and our custom field phone, and you can see that we have added the data and that the data format for this column is String in the table header
Other fields can also be created to the MongoDB database as described above.
Pointer types
Now that we can create our table data format visually in the Parse-Dashboard dashboard, we can add a field of User to the Game table created in the previous section and declare a type value of Pointer to the user table
-
Then we can see that the user field in the Game table defaults to undefined
-
Clicking on the User table for the first data in the Game changes the table to an input box for the objectId of the user we have created
-
After the mouse mouse is removed, the table content is automatically saved to the parse-server database, and the table is displayed as a link button. Click the button to directly access the User data in the User table, which makes it easy to connect multiple data tables. The ability to easily view data from a table is one of the benefits of Pointer, a data type supported by Parse-Server and optimized for display by Parse-Dashboard
Structure of the output
Table structures can be created using mouse clicks in parse-Dashboard, so how do we view the raw data structures of the tables we created? We can see the table deconstruction type using MongoDB’s command-line tool.
-
Go to MongoDB’s interactive command line
mongo Copy the code
-
Viewing the database list
use test; show tables; Copy the code
The output
> use test switched to db test > show tables Game _Idempotency _Role _SCHEMA _Session _User > Copy the code
As you can see from the table, parse-Server’s database has several tables starting with an underscore by default:
_Idemotency
Index value of the database_Role
The role table, i.eparse-dashboard
See in theRole
The data table_SCHEMA
The format definitions for all data tables are important in this section_Session
User loginsession
Table that identifies the user’s login status_User
User table, i.eparse-dashboard
See in theUser
The data table
-
View the format definition of the data table _SCHEMA
db.getCollection('_SCHEMA').find().pretty() Copy the code
Since _SCHEMA starts with an underscore, we need to use the getCollection method provided by MongoDB to get the instance of the table
Output:
{ "_id" : "_User", "objectId" : "string", "updatedAt" : "date", "createdAt" : "date", "username" : "string", "email" : "string", "emailVerified" : "boolean", "authData" : "object", "_metadata" : { "indexes" : { "_id_" : { "_id" : 1 }, "username_1" : { "username" : 1 }, "email_1" : { "email" : 1 }, "case_insensitive_username" : { "username" : 1 }, "case_insensitive_email" : { "email" : 1 } }, "fields_options" : { "phone" : { "required" : true, "defaultValue" : "null" } } }, "phone" : "string" } { "_id" : "_Role", "objectId" : "string", "updatedAt" : "date", "createdAt" : "date", "name" : "string", "users" : "relation<_User>", "roles" : "relation<_Role>", "_metadata" : { "indexes" : { "_id_" : { "_id" : 1 }, "name_1" : { "name" : 1 } } } } { "_id" : "Game", "objectId" : "string", "updatedAt" : "date", "createdAt" : "date", "score" : "number", "cheatMode" : "boolean", "playerName" : "string", "_metadata" : { "indexes" : { "_id_" : { "_id" : 1 } }, "fields_options" : { "user" : { "required" : true } } }, "user" : "*_User" } { "_id" : "_Session", "objectId" : "string", "updatedAt" : "date", "createdAt" : "date", "restricted" : "boolean", "user" : "*_User", "installationId" : "string", "sessionToken" : "string", "expiresAt" : "date", "createdWith" : "object" } Copy the code
In the _User column, we can see the definition of the phone field and the default setting:
"fields_options" : { "phone" : { "required" : true, "defaultValue" : "null" } } Copy the code
Therefore, in addition to adding and modifying table structures in parse-Dashboard, we can also use other MongoDB tools to make changes to raw data during development.
Backup and Restore
Usually after designing the database, I will back up the _SCHEMA table of the database so that we can initialize our database structure on another server.
-
Backup _SCHEMA
mongodump -d test -c _SCHEMA -o ./backup Copy the code
The mongodump command is usually used to back up the MongoDB database, indicating that the specified data of the database is fully backed up in Bson format.
-d
Indicates which database to back up-c
Represents which table in the database to back up-o
In which directory is the file backed up
After the backup is complete, you can see that the test directory is added to the backup directory, where the _SCHEMA data is backed up
-
Restore _SCHEMA
In the server of the database we are initializing, we need to restore the _SCHEMA table structure of the database via MongoRestore
mongorestore -d test -c _SCHEMA --dir ./backup/test Copy the code
Mongorestore’s parameters are basically similar to mongodump’s except that mongoRestore’s –dir parameter controls the directory of files to be backed up and restored
-d
Indicates which database to back up-c
Represents which table in the database to back up--dir
Back up the restored file directory
As long as we can put the _SCHEMA table structure in the project, we can easily use the tool to back up and restore data, specific other MongoDB usage methods and tools can be seen in the official MongoDB documentation