3

I'm using php sdk to delete some fields from firestore database, i want to delete a map from an array inside a document, but instead the function used delete all maps inside the parent array.

My firestore database looks like this firebase database

What i'm trying to do is remove a specific index ex: index 0 with it's children from imageUrls array not all the maps inside it.

I tried these 2 functions :

$usersRef->update([
        ['path' => 'imageUrls.image_url', 'value' => FieldValue::arrayRemove(['image.png'])]
    ]);

and this one

$usersRef->update([
    ['path' => 'imageUrls.image_url', 'value' => FieldValue::deleteField()]
]);

The first function remove all imageUrls childrens and change imageUrls type from array to map, while the second one nothing happened. All the fields still exist in the document and no deletion occurd

<?php 

namespace App\Services;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FieldValue;
use Google\Cloud\Firestore\FieldPath;

class FirebaseService
{
    public function delete()
    {
        // Create the Cloud Firestore client
        $db = new FirestoreClient(
            ['projectId' => 'MyProjectId']
        );

        $usersRef = $db->collection('NewStories')->document('1');

        $usersRef->update(
                         [
                              ['path' =>'imageUrls.image_url', 
                              'value' => FieldValue::arrayRemove(['image.png'])]
                         ]);  
    }
}
1
  • I've just tried out the second option ( FieldValue::deleteField() ) and it worked for me. Commented Dec 25, 2020 at 21:05

1 Answer 1

1

This can be achieved using the arrayRemove() method. As the PHP Client for Firestore says

Returns a special value that can be used with set(), create() or update() that tells the server to remove the given elements from any array value that already exists on the server. All instances of each element specified will be removed from the array. If the field being modified is not already an array it will be overwritten with an empty array.


Update:

Firebase does not support updating an existing element in an indexed array. More information can be found in the Official Documentation.

Workaround:

Reading the entire array out of the document, make modifications to it in memory, then update the modified array field entirely.**

Credits to this Firestore Update single item in an array field case.

Sign up to request clarification or add additional context in comments.

10 Comments

As i mention in description that arrayRemove is deleteing all image_url inside imageUrls even i'm giving it the exact name. also as per docummentation // falsey is good enough unless the field is actually 0. if (!$field && $field !== 0) so i can't access element at index 0
Please check out this similar OS case
It didn't help, they didn't give him a solution instead they ask to update document but remain the index zero is the problem
I will try to explain conceptually what could work for you. You will need to get the reference of document "1", which you already have. Then, you will need to access de array imgeUrls. After, you will have go to one level deeper and access the nested array "0" or "1", as I can see in the image, to be able to update and erase the field "image_url" with the value "image.png". This may give you an idea code wise here
I don't know how firebase handle the arrayRemove i'm using this $usersRef = $db->collection('NewStories')->document(1); $usersRef->update([ ['path' => 'imageUrls.1', 'value' => FieldValue::arrayRemove(['svg.jpg'])] ]); and it's actually removing children index (1) ignoring if value exist or not. Weird isn't
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.