Nested Values vs Master List

Hi,

I’m still new to NoSQL and I was wondering how we deal with master list maintenance and duplicated values?

I have a product that belongs to a category. Currently, the document looks like this:

{
   productId: "12335456",
   productName: "Mustard",
   categoryId: "12345",
}

We have a master list for the categories. However, it’s a bit of a pain to call another query just to get the category’s name for all products and this is done in an iPad. There’s a bit of a performance concern. I’ve read that you can “nest” data in the document:

{
   productId: "12335456",
   productName: "Mustard",
   category: "Condiments",
}

This is better when fetching products because the category name is already included. And it’s only a string so it doesn’t really affect the document’s footprint.

However, how do we maintain the “Master List”? What if I wanted to rename “Condiments” to “Condiments & Dips”? Please note that we’re using couchbase mobile 1.4.0. We don’t have N1QL.

This is a classic case of normalizing (by reference) or de-normalizing (embed) your data and the trade offs of redundancy and performance . In your case, as you pointed out, since the category is just a string, there isn’t really any impact to the data footprint.

Others can chime in, but I can’t think of any way out other than for you to have to update all related documents if the category name changes (have to do it atomically). This is probably the benefit of using an Id instead of string.

That said , it depends on how often you expect the master list to change ? If this is relatively rare occurrence, then its not a big deal. Its likely that you are probably going to be fetching product details much more often then updating the master list.

We decided to just embed the category. Do you know a tutorial or maybe technique on how to change all the occurences when we want to change the category name?

It’s straightforward. Query for documents where category == “Condiments”, then iterate over the documents and change the category property. Do this in an inBatch operation for speed and atomicity.

1 Like