Reputation: 3120
So in a traditional database I might have 2 tables like users, company
id | username | companyid | |
---|---|---|---|
1 | j23 | 1 | [email protected] |
2 | fj222 | 1 | [email protected] |
id | ownerid | company_name |
---|---|---|
1 | 1 | A Really boring company |
This is to say that user 1 and 2 are apart of company 1 (a really boring company) and user 1 is the owner of this company.
I could easily issue an update statement in MySQL or Postgresql to update the company name.
But how could I model the same data from a NoSQL perspective, in something like Dynamodb or Mongodb?
Would each user record (document in NoSQL) contain the same company table data (id, ownerid (or is owner true/false, and company name)? I'm unclear how to update the record for all users containing this data then if the company name needed to be updated.
Upvotes: 1
Views: 1370
Reputation: 116
Depends on the programming language used to handle NoSQL objects/documents you have variety of ORM libraries to model your schema. Eg. for MongoDB plus JS/Typescript I recommend Mongoose and its subdocuments. Here is more about it:
https://mongoosejs.com/docs/subdocs.html
Upvotes: 0
Reputation: 7424
In case you want to save the company object as JSON in each field (for performance reasons), indeed, you have to update a lot of rows.
But best way to achieve this is to have a similar structure as you have above, in MySQL. NoSql schema depends a lot on the queries you will be making.
For example, the schema above is great for:
User
by username
(you can add an index), get the companyId
and do another query on Company
to fetch the name.Embedded company JSON would work better for:
User
s by city
and then another query for all users found to fetch the company nameWhat if company name changes ofter and I want to get users by city?
Upvotes: 1