I have been using firebase RTDB and I am going to migrate it to firestore.
I had userIds associated with many other nodes.
For example, I have users
, messages
, purchases
.
To associate user with other collections, I had users_messages/$date/$userId/$messageId
, purchases/$date/$userId/$purchaseId
, because RTDB did not support plex queries and I wanted to be able to query messages between date range for a specific user.
Now I realized firestore is way better than RTDB in terms of data querying, and going to have messages
, purchases
collection with date
and user
.
In this case, should I store userId
or whole user
document in messages
and purchases
collection?
I have been using firebase RTDB and I am going to migrate it to firestore.
I had userIds associated with many other nodes.
For example, I have users
, messages
, purchases
.
To associate user with other collections, I had users_messages/$date/$userId/$messageId
, purchases/$date/$userId/$purchaseId
, because RTDB did not support plex queries and I wanted to be able to query messages between date range for a specific user.
Now I realized firestore is way better than RTDB in terms of data querying, and going to have messages
, purchases
collection with date
and user
.
In this case, should I store userId
or whole user
document in messages
and purchases
collection?
2 Answers
Reset to default 5In a very general sense, the best way that you store duplicate data in a NoSQL database is pletely dependent on your project's requirements. There is not one correct solution for everyone.
You have to ask some questions about the data you want to duplicate:
- Is it static, or will it change over time?
- If it does change, do you need to update every duplicated instance of the data so they all stay in sync?
- Are you optimizing for performance or cost?
If your duplicated data needs to change and stay in sync, then you might have a hard time keeping all those duplicates up to date, and you might spend a lot of money keeping all those documents fresh, as it will require a read and write for each document for each change.
If you need your queries to be very fast, you may want to prefer to duplicate more data so that the client only has to read one document per item queried, rather than multiple documents. But you may also be able to depend on local client caches makes this cheaper, depending on the data the client has to read.
In the end, for your choice of whether or not to duplicate some data, it is highly dependent on your data and its characteristics. You will have to think that through on a case by case basis.
You can simply put in userId
and define a new collection users
which contains the description of each userId
as a document and it's associated data.
Something on the lines of -
- messages -> date -> userid -> messageid
- purchases -> date -> userid -> purchaseid
- users -> userid -> details
PS: Just a tip, a document can refer to a collection as well. For example, collection -> document -> collection -> document.. and so on..