In a nosql database multiple items are stored with the category and subcategory in the format given below.
Category: [
{id: "c2", subCategories: [ {id: "s2", items: [ {id: i3}, {id: i4} ] } ] }
{id: "c1", subCategories: [ {id: "s1", items: [ {id: i1}, {id: i2} ] } ] }
]
Problem is to add new items with optimised time complexity, we get the same structure to add new items and we have to find and update the subcategories with the new items.
Let's say we get this
Category: [
{id: "c2", subCategories: [ {id: "s2", items: [ {id: i5} ] } ] }
{id: "c1", subCategories: [ {id: "s1", items: [ {id: i6}, {id: i7} ] } ] }
]
then the result should be
Category: [
{id: "c2", subCategories: [ {id: "s2", items: [ {id: i3}, {id: i4}, {id: i5} ] } ] } ]
{id: "c1", subCategories: [ {id: "s1", items: [ {id: i1}, {id: i2},{id: i6}, {id: i7} ] }]
],
currently the approach I can think of, is to iterate over the data with multiple categories and subcategories the time complexity goes to n开发者_运维知识库3 and then after getting the item which is to be added we have to find out where we need to add that item it further increases, is there a way we could reduce this complexity ?
精彩评论