- Building Enterprise JavaScript Applications
- Daniel Li
- 293字
- 2021-07-23 16:31:28
Document vs. relationship data storage
For example, your application may be a school directory, storing information about schools, users (including teachers, staff, parents, and students), exams, classrooms, classes, and their relations with each other. Given that the data structure can be kept relatively flat (that is, mostly simple key-value entries), a relational database would be most suitable.
On the other hand, if you're building a social network, and want to store a user's settings, a document database may be more suitable. This is because the settings may be quite complex, such as the one shown here:
{
"profile": {
"firstName": "",
"lastName": "",
"avatar": "",
"cover": "",
"color": "#fedcab"
},
"active": true,
"notifications": {
"email": {
"disable": false,
"like": true,
"comment": true,
"follow": true
},
"app": {
"disable": false,
"like": true,
"comment": true,
"follow": true }
}};
With a relational database, you'll have to establish naming conventions for the columns (such as settings.notification.app.follow) in order to retain hierarchical information. However, to use the settings, you'll have to manually reconstruct the object before you can work with it. You'll need to do this each time the entry is retrieved.
Storing this user information as a document allows you to store objects as they are, retaining their structure, and retrieve them as they are, without having to do extra work.
However, if your intention is to structure your data in a non-relational way, you'd be better off using a NoSQL database from the start. I'd recommend storing documents in a traditional relational database only when you have existing data and you are adding a new data structure on top of it.