最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you store duplicate data in firestore? - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Oct 22, 2018 at 10:52 emil asked Oct 22, 2018 at 10:37 emilemil 6,3644 gold badges32 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

In 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:

  1. Is it static, or will it change over time?
  2. If it does change, do you need to update every duplicated instance of the data so they all stay in sync?
  3. 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 -

  1. messages -> date -> userid -> messageid
  2. purchases -> date -> userid -> purchaseid
  3. 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..

发布评论

评论列表(0)

  1. 暂无评论