1

I'm making an app with database structure like this:

{
    "Locations": {
      "location1": {
        "name": "Nice location"
      }
    },

    "User_posts": {
      "user1": {
         "post1": {
         "location_name": "Nice location",
         "location_id": "location1",
         "description": "Wow!"
        },
         "post2": {
         "location_name": "Nice location",
         "location_id": "location1",
         "description": "Nice"
        }
    }
}

If I have to change location1 name, how to change all location_name's that all users posts have? I have to download all the data before and update it or there is other method?

I think that using location id only to get location name for every location when user enters his posts is not a good idea.

dojei
  • 13
  • 3
  • You should look at this https://www.youtube.com/watch?v=WacqhiI-g_o&list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s it will make everything clear – cutiko Aug 16 '17 at 14:38

2 Answers2

1

By duplicating data you improve your read performance/scalability at the cost of decreased write performance. This is a normal trade-off in NoSQL databases and in highly scaleable systems in general.

If you want to update the location_name of all posts, you will indeed have to query the posts and update each. If you need to do this regularly, consider keeping a separate lookup list for each location to find the posts where it used. Such an inverted index is another common occurrence in NoSQL databases.

I covered strategies for updating the duplicated data in my answer here: How to write denormalized data in Firebase

Coming from a relational/SQL background, this may initially feel uncomfortable, since it goes against the normalization rules we've been taught. To counter that feeling, I recommend reading NoSQL data modeling, watching Firebase for SQL developers and in general just read some more NoSQL data modeling questions.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

You can add one more attribute to location1 , say isLocationOf , which will store all the user id or perhaps post id/post names. Like

"Locations": {
  "location1": {
    "name": "Nice location",
    "isLocationOf": {
               'post1': true,
               'post2': true
                     }
  }
}

Here isLocationOf is an attribute of Locations whose value is an object.
Now if locations1's name gets changed then you can retrieve its isLocationOf object , iterate through it , get all posts id/name containing that location.Then use the post ids to update all entries having this address .
       Also whenever you add new post , you have to add its post id/name to isLocation object.

Satish Kumar
  • 601
  • 6
  • 14