Android Firebase : Update/Add record in Nested Array data

泄露秘密 提交于 2021-02-04 19:10:30

问题


I am stuck in one case of firebase operation in android. My Requirement :

I have table Named : "Values" This value table contains following type of data.

  {
    "menu": [
             {
               "category": "cocktails",
               "product": [
                            {
                              "id": "123",
                              "name": "test1"
                            },
                            {
                              "id": "456",
                              "name": "test2"
                            }
                          ]
            },
            {
               "category": "vodka",
               "product": [
                            {
                               "id": "789",
                               "name": "test3"
                             },
                             {
                                "id": "901",
                                "name": "test4"
                             }
                         ]
              }
           ]
       }

Now, I want to update id : 123 with name = "test5" , How to update this nested array object in firebase database in android?

I tried following but it update/add Entire menu array not perticular product array object.

I just want to update in product array object.

Following are the code that i tried, but it update all menu array.

      val listPorduct = ArrayList<HashMap<String, Any>>()

      for ((i, product) in productList.withIndex()) {

            var productMap = HashMap<String, Any>()

              productMap = hashMapOf(
                "id" to "123",
                "name" to "test5")

             listPorduct.add(productMap)
       }

        val map = hashMapOf<String, Any>()
        map.put("category", list[position].category)
        map.put("product", listPorduct)

       repository.getTabs().update("menu", FieldValue.arrayUnion(map))

if i am try with

       repository.getTabs().update("menu/product", FieldValue.arrayUnion(map))

       or

       repository.getTabs().update("menu.product", FieldValue.arrayUnion(map))

then getting issue / or dot not allowed. i have latest firebase gradle file.

How update particular position of product object? Please anyone can help me to solve out this?

Image of firebase database.


回答1:


The solution to this problem can be found in my answer from the following post:

  • firestore -- Add element in a field in hashmap

There is a slightly smaller difference. However, for your use-case, please note that the document that you gave us as an example contains a property named menu which is of type array. In this array, you have added some objects that contain two properties, a String property named category and an array named product. This array contains in term two other properties, one is the id and the other one is the name, both being of type String.

None of those arrays can be updated using FieldValue.arrayUnion(map). The only option that you have is to get the menu array as a list of HashMap<String, Any>. Once you have this map, you can simply iterate through it and get the desired data.

So to update the value of a property inside a Map, you should first get the map, do the changes and put it back. In the end, after you've done all the necessary changes, write the document back to Firestore.

Edit:

According to your comment:

In my case I have more than 5000 records in a list with a nested array.

It's hard to believe that you can nest 5000 elements into a single document because it might not fit since the maximum size for a document is 1 MiB (1,048,576 bytes). Please see usage and limits. So nesting 5000 objects into an array isn't a solution at all. In this case, the single solution that you have is to use a sub-collection and add each item from the array as a document. In this case, there are no limitations.

As a conclusion, the only highly scalable way to store an unknown large list of items is using documents in a collection. Array type fields do not scale for lists the are constantly growing because the items will eventually exceed that (1 MiB), which will obviously cause problems in the future.



来源:https://stackoverflow.com/questions/59629472/android-firebase-update-add-record-in-nested-array-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!