'How to have an active flag in NoSQL without transaction
This question is only for understanding purpose. This might be a noob question.
Assume that I have a tabular or document NoSQL database which do not support transactions. And I have a locations table/document which has an is_active column. A user can have multiple locations, but can have a SINGLE is_active:true location only. Now, if the user wants to
- Change an
is_activelocation, how do we handle it? As I need to setis_activefalse for 1 row and true for another - A use case where a user wants to create a new location for himself and set that location as
is_active
How do I handle these logics without a transaction in a NoSQL? Do I need to model my tables in some other way?
Let's assume that:
- I cannot use an SQL
- I should not use transaction support provided by DBs like Mongo
NOTE: These might be a lot of assumptions and might not be real-world use cases. But I am just trying to understand HOW we should model NoSQL databases.
Solution 1:[1]
This is a typical data modelling question for nosql systems. I won't put here the whole theory, but these are links to check: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-relational-modeling.html and https://cassandra.apache.org/doc/latest/cassandra/data_modeling/index.html
The key take away is that you don't want to create a normalized version of SQL like db in a nosql system. In your particular case, you have to explore access patterns for reads and writes; and that will help you to define you data structures. For example, you may want to get rid of "locations" table and keep those as properties of users - this is almost classical use case in nosql world.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AndrewR |
