简体   繁体   中英

Modify the value of a single element in an array - firestore

I have the following set-up for a data structure:

users
|-name
|-id

businesses
|-name
|-id
|-owners
||[0]
||-name
||-id
||[1]
||-name
||-id

reviews
|-id
|-description
|-authorId
|-authorName

Based on the videos and blog posts, I know that I can remove elements and add elements using the new array functionality from 2018. Is there a way to pass a specific reference to the update method to update a specific value, like owner[1].name?

Watched the video on Multipath updates, read the blog post on arrays, searched around for examples of doing multipath updates, attempted to read about different methods of updating arrays on StackOverflow.

    db.collection("businesses").whereArrayContains("owners",userId).get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        DocumentReference ref =db.collection("businesses").document(documentSnapshot.getId());
                        batch.update(ref, "Name", editText.getText().toString().trim());
                    }

No, the native array operations only operate on entire items within a list type field. You can't call out a specific property of an object in an array. If you want to modify a property of an object in a list, you have to read the document, modify the list in your code, then update the entire field back to the document. You will not be able to do this with a single operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM