0

NoSQL noob here. I'm building an app using Firestore NoSQL. I'm looping through items where every item has a owner id (creator user id). I want to display owner's name on the listing page. In traditional SQL, i have foreign key so I can just make reference to say, Item.Owner.FirstName

What's the best practice in NoSQL? Should I be saving owner name as a field at the time of saving the item? or do a lookup of each owner id to get user object whilst i'm looping through items?

Second option sounds expensive so i'm assuming 1st way is the way to go. Unless there's a better, more accepted way?

DrZeuso
  • 181
  • 2
  • 10
  • You can check also **[this](https://stackoverflow.com/questions/54258303/what-is-denormalization-in-firebase-cloud-firestore)** out. – Alex Mamo Mar 05 '20 at 09:45

1 Answers1

1

Both will work. You either reference the data in the other document in whatever way you see fit, or you duplicate information into the document that you intend to query to build the display. You just have to decide what which problem you want to deal with:

  • If you duplicate data among documents (known as "denormalization"), then you'll have to put effort into keeping them all up to date with each other, if that's what you require. So, writing one document might actually turn into writing multiple documents.
  • If you normalize your data with no duplication, then each of your queries will require more queries to get the related data from other documents. This could result in a drop in performance and an increase in cost for apps with heavy read loads.

Since we don't know the performance requirements and usage behavior of your app, there is no way to give specific advice. You will have to think carefully about which problem you want to have, perhaps based on complexity, performance, and overall cost.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441